﻿using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using OpenCvSharp.Internal;
using System.Linq;
using System.Runtime.InteropServices;
using System.Threading;
using VIA.Integration;
using VIA.Integration.Alarms;
using VIA.Integration.Domain;
using VIA.Integration.Domain.Production;
using VIA.Integration.LeadShine;
using VIA.Project.Domain.Services;
using VIA.SiMoJi.Domain.Electronics;
using VIA.SiMoJi.Domain.Option;
using VIA.SiMoJi.Domain.Vision;
using VMathNet.Numerics;
using static OpenCvSharp.Stitcher;

namespace VIA.SiMoJi.Domain.Inspection
{
    public class GongLiaoStation : StationBase<GongLiaoStationOption>, ISuportClearAlarm, IRefreshing
    {
        #region  字段
        public volatile VisionStation Vision;
        public BuJinPiDai BuJinPiDaiStation;
        private readonly ICurrentRecipe _recipe;
        private const string Ds_顶升位 = "顶升位";
        private const string Ds_待机位 = "待机位";
        private const string Bj_待机位 = "待机位";
        private const string Bj_托盘位 = "托盘位";
        private bool feedTrayLoadError;
        private bool feedTrayUnloadError;
        private bool prouductNotRegisteredError;
        private bool prouductRegisteredClearError;
        private bool prouductJoinError;
        private InspectorOption _inspectorOption;
        private bool _isInitialized;
        private ICalcTimeService _calcTimeService;
        private IOptionRepository _optionRepository;
        #endregion

        #region 事件
        public event EventHandler<string> StepsChanged;
        /// <summary>
        /// 人工确认
        /// </summary>
        public event Func<string, Task<bool>> ManualStepsConfirmChanged;
        #endregion

        #region 设备报警
        [Alarm(true, AlarmLevel.Error, "供料盘拍照报警-入料", Logical.Equal)]
        public bool FeedTrayLoadError
        {
            get => feedTrayLoadError;
            set => NotifyAlarm(ref feedTrayLoadError, value);
        }
        [Alarm(true, AlarmLevel.Error, "供料盘拍照报警-下料", Logical.Equal)]
        public bool FeedTrayUnloadError
        {
            get => feedTrayUnloadError;
            set => NotifyAlarm(ref feedTrayUnloadError, value);
        }
        [Alarm(true, AlarmLevel.Error, "供料盘未在籍", Logical.Equal)]
        public bool ProuductNotRegisteredError
        {
            get => prouductNotRegisteredError;
            set => NotifyAlarm(ref prouductNotRegisteredError, value);
        }
        [Alarm(true, AlarmLevel.Error, "供料盘在籍已清除,Tray盘未拿走", Logical.Equal)]
        public bool ProuductRegisteredClearError
        {
            get => prouductRegisteredClearError;
            set => NotifyAlarm(ref prouductRegisteredClearError, value);
        }
        [Alarm(true, AlarmLevel.Error, "供料盘连扳", Logical.Equal)]
        public bool ProuductJoinError
        {
            get => prouductJoinError;
            set => NotifyAlarm(ref prouductJoinError, value);
        }
        #endregion

        #region 硬件设备
        public Electronic Electronic => Electronic.Default;
        [MotionPath(Bj_待机位)]
        [MotionPath(Bj_托盘位)]
        public DmcAxis BianJu_Axis { get; }
        [MotionPath(Ds_顶升位)]
        [MotionPath(Ds_待机位)]
        public DmcAxis DingSheng_Axis { get; }
        public BuJinPiDai PiDai { get; }
        [MotionPath(BuJinPiDai._运行高速)]
        [MotionPath(BuJinPiDai._运行低速)]
        public DmcAxis Axis_PiDai1 { get; }
        [MotionPath(BuJinPiDai._运行高速)]
        [MotionPath(BuJinPiDai._运行低速)]
        public DmcAxis Axis_PiDai2 { get; }
        public CylinderEx Cylinder_阻挡气缸1 => Electronic.Cylinder_入料到位阻挡气缸1;
        public CylinderEx Cylinder_阻挡气缸2 => Electronic.Cylinder_入料到位阻挡气缸2;
        public CylinderEx Cylinder_左右气缸1 => Electronic.Cylinder_入料左右定位气缸1;
        public CylinderEx Cylinder_左右气缸2 => Electronic.Cylinder_入料左右定位气缸2;
        public CylinderEx Cylinder_前后气缸1 => Electronic.Cylinder_入料前后定位水平气缸1;
        public CylinderEx Cylinder_前后气缸2 => Electronic.Cylinder_入料前后定位水平气缸2;
        public CylinderEx Cylinder_上下气缸1 => Electronic.Cylinder_入料前后定位上下气缸1;
        public CylinderEx Cylinder_上下气缸2 => Electronic.Cylinder_入料前后定位上下气缸2;
        #endregion

        #region 构造函数
        public GongLiaoStation()
        {
            Axis_PiDai1 = new(Electronic.Controller) { Id = nameof(Axis_PiDai1), NoId = 1, Name = "入料皮带步进1" };
            Axis_PiDai2 = new(Electronic.Controller) { Id = nameof(Axis_PiDai2), NoId = 2, Name = "入料皮带步进2" };
            PiDai = new(Axis_PiDai1, Axis_PiDai2) { Id = nameof(BuJinPiDai), Name = "入料皮带" };
            RetryCount = 0;
            this.InitalizeAlarms();
            LogGroup = LoggerCategory.GongLiao;
            BianJu_Axis = new(Electronic.Controller) { Id = nameof(BianJu_Axis), NoId = 0, Name = "入料变距伺服" };
            DingSheng_Axis = new(Electronic.Controller) { Id = "Gongliao" + nameof(DingSheng_Axis), NoId = 3, Name = "入料顶升步进", CanMove = IsFoolProofingJackingAxis };
            Cylinder_上下气缸1.CanMove = IsFoolProofingLoadTopBotCylinder;
            Cylinder_上下气缸2.CanMove = IsFoolProofingLoadTopBotCylinder;
            Add(BianJu_Axis, DingSheng_Axis);
            Add(PiDai.Axis_PiDai1, PiDai.Axis_PiDai2, PiDai);
            Add(Cylinder_阻挡气缸1);
            Add(Cylinder_阻挡气缸2);
            Add(Cylinder_左右气缸1);
            Add(Cylinder_左右气缸2);
            Add(Cylinder_前后气缸1);
            Add(Cylinder_前后气缸2);
            Add(Cylinder_上下气缸1);
            Add(Cylinder_上下气缸2);
        }
        #endregion

        #region 初始流程
        public bool IsInitialized => _isInitialized;
        /// <summary>
        /// 产品是否在籍
        /// </summary>
        public bool IsProuductRegistered => Status.HasFeedTray && Z轴有无料光电 && 入料到位光电;
        public async Task<bool> InitializeStation(CancellationToken cancellationToken)
        {
            try
            {
                OnStepChanged("初始化中");
                _isInitialized = false;
                LogInfo("初始化->[开始]");
                if (IsProuductRegistered == false)//产品不在籍情况
                {
                    if (IsProductLine)//如果流水线有料
                    {
                        if (Step == GongLiaoStationRunStep.Step11_皮带转动等待下料完成)
                        {

                        }
                        else
                        {
                            if ((入料到位光电 || 入料到位减速光电) && DingSheng_Axis.CheckIsInPosition(Ds_待机位))
                            {
                                LogInfo($"初始化->[皮带上有料皮带反转动,开始]");
                                _ = PiDai.LowNegativeRunning(cancellationToken);
                                await Judger.Sure(() => 入料到位减速光电 == false, cancellationToken);
                                await PiDai.Stop();
                                LogInfo($"初始化->[皮带上有料皮带反转动,停止]");
                                if (入料到位光电 == false && 出料口光电 == false)//没有感应到产品
                                {
                                    if (Cylinder_阻挡气缸1.IsBack())
                                        await Cylinder_阻挡气缸1.PushOutAsync(token: cancellationToken);
                                    if (Cylinder_阻挡气缸2.IsBack())
                                        await Cylinder_阻挡气缸2.PushOutAsync(token: cancellationToken);
                                }
                                LogInfo($"初始化->[皮带上有料皮带正转动,开始]");
                                _ = PiDai.LowPostiveRunning(cancellationToken);
                                await Judger.Sure(() => 入料到位光电, cancellationToken);
                                await Task.Delay(motionTimeOption.ArriveSensorDelay, cancellationToken);
                                await PiDai.Stop();
                                LogInfo($"初始化->[皮带上有料皮带正转动,停止]");
                            }
                            else if ((Z轴有无料光电 || 入料口光电) && DingSheng_Axis.CheckIsInPosition(Ds_待机位))
                            {
                                LogInfo($"初始化->[皮带上有料皮带正转动,开始]");
                                _ = PiDai.LowPostiveRunning(cancellationToken);
                                await Judger.Sure(() => 入料到位光电, cancellationToken);
                                await Task.Delay(motionTimeOption.ArriveSensorDelay, cancellationToken);
                                await PiDai.Stop();
                                LogInfo($"初始化->[皮带上有料皮带正转动,停止]");
                            }
                        }
                    }
                    else
                    {
                        if (Step == GongLiaoStationRunStep.Step11_皮带转动等待下料完成)
                        {

                        }
                        else
                        {
                            if (入料到位光电 == false && 出料口光电 == false)//没有感应到产品
                            {
                                if (Cylinder_阻挡气缸1.IsBack())
                                    await Cylinder_阻挡气缸1.PushOutAsync(token: cancellationToken);
                                if (Cylinder_阻挡气缸2.IsBack())
                                    await Cylinder_阻挡气缸2.PushOutAsync(token: cancellationToken);
                            }
                        }
                    }
                }
                LogInfo($"初始化->[产品在籍{IsProuductRegistered}]");
                List<bool> init = new List<bool>();
                PiDai.LoadYouClose();
                PiDai.UnloadYouClose();
                var boolA = await InitializeCylinder(cancellationToken);
                var boolB = await InitializeAxis(cancellationToken);
                if (IsProuductRegistered == false)//产品不在籍所以重新上一盘新的料
                {
                    Step = GongLiaoStationRunStep.Step01_阻挡气缸伸出;
                }
                _isInitialized = boolA && boolB;
                if (_isInitialized)
                {
                    OnStepChanged("初始化完成");
                    LogInfo("初始化->[完成]");
                }
                else
                {
                    OnStepChanged("初始化失败");
                    LogInfo("初始化->[失败]");
                }
            }
            catch (Exception ex)
            {
                _isInitialized = false;
                OnStepChanged("初始化失败");
                LogError($"初始化->[异常]{ex.Message}", ex);
            }
            return _isInitialized;
        }
        /// <summary>
        /// 初始化气缸
        /// </summary>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        async Task<bool> InitializeCylinder(CancellationToken cancellationToken)
        {
            var cylinders = Automatics.Where(x => x is CylinderEx).Cast<CylinderEx>().ToArray();
            bool bianJuInPosition = BianJu_Axis.CheckIsInPosition(Bj_托盘位);
            bool dingShenInPosition = DingSheng_Axis.CheckIsInPosition(Ds_顶升位);
            if (IsProuductRegistered && bianJuInPosition && dingShenInPosition)
            {
                var notCylinders = cylinders.Where(x => x.IsPushOut() == false);
                if (notCylinders.Any())
                {
                    var notCylinderNames = notCylinders.Select(x => x.Name);
                    LogError($"初始化->[{string.Join('|', notCylinderNames)}不在动点]");
                    return false;
                }
                else
                {
                    LogInfo($"初始化->[气缸已在动点]");
                    return true;//产品是否在籍
                }
            }
            else
            {
                LogInfo($"初始化->[气缸原点开始]");
                await Task.WhenAll(Cylinder_前后气缸1.TakeBackAsync(token: cancellationToken), Cylinder_前后气缸2.TakeBackAsync(token: cancellationToken), Cylinder_左右气缸1.TakeBackAsync(token: cancellationToken), Cylinder_左右气缸1.TakeBackAsync(token: cancellationToken));
                if (!Cylinder_前后气缸1.IsBack() || !Cylinder_前后气缸2.IsBack())
                {
                    LogError($"初始化->[前后气缸原点失败]");
                    return false;
                }
                if (Electronic.DI21_入料到位光电.Value && Cylinder_阻挡气缸1.IsPushOut() && Cylinder_阻挡气缸2.IsPushOut())//表示流线到位这里有料,就不复位阻挡气缸了
                {
                    await Task.WhenAll(Cylinder_上下气缸1.TakeBackAsync(token: cancellationToken), Cylinder_上下气缸2.TakeBackAsync(token: cancellationToken));
                    if (!Cylinder_上下气缸1.IsBack() || !Cylinder_上下气缸2.IsBack())
                    {
                        LogError($"初始化->[上下气缸原点失败]");
                        return false;
                    }
                }
                else
                {
                    var cylinders2 = cylinders.Where(x => x.IsPushOut());
                    await Task.WhenAll(cylinders2.Select(x => x.TakeBackAsync(token: cancellationToken)));
                    if (cylinders2.Any(cy => cy.IsBack() == false))
                    {
                        LogError($"初始化->[气缸原点失败]");
                        return false;
                    }
                    LogInfo($"初始化->[气缸原点结束]");
                }
                return true;
            }
        }
        /// <summary>
        /// 初始化轴(产品在籍->不回原判断轴是不是在当前位|产品不在籍->回原所有轴，然后去各自待机位)
        /// </summary>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        async Task<bool> InitializeAxis(CancellationToken cancellationToken)
        {
            var axies = Automatics.Where(x => x is DmcAxis).Cast<DmcAxis>().ToArray();
            LogInfo($"初始化->[轴重置开始]");
            axies.ForEach(x => x.Reset());
            axies.ForEach(x => x.ServoOn(true));
            LogInfo($"初始化->[轴重置结束]");
            bool bianJuInPosition = BianJu_Axis.CheckIsInPosition(Bj_托盘位);
            bool dingShenInPosition = DingSheng_Axis.CheckIsInPosition(Ds_顶升位);
            if (IsProuductRegistered)
            {
                if (!bianJuInPosition)
                {
                    LogInfo($"初始化->[{BianJu_Axis.Name}[在籍]回原开始]");
                    await BianJu_Axis.MoveOriginAsync(cancellationToken);
                    LogInfo($"初始化->[{BianJu_Axis.Name}[在籍]回原结束]");
                    //变距轴移动托盘位
                    LogInfo($"初始化->[{BianJu_Axis.Name}移动->[在籍][Bj_托盘位]开始]");
                    await BianJu_Axis.MoveToAsync(Bj_托盘位, cancellationToken);
                    if (!BianJu_Axis.CheckIsInPosition(Bj_托盘位))
                    {
                        LogError($"初始化->[{BianJu_Axis.Name}移动->[在籍][Bj_托盘位]失败]");
                        return false;
                    }
                    LogInfo($"初始化->[{BianJu_Axis.Name}移动->[在籍][Bj_托盘位]完成]");
                }
                if (!dingShenInPosition)
                {
                    LogInfo($"初始化->[{DingSheng_Axis.Name}[在籍]回原开始]");
                    await DingSheng_Axis.MoveOriginAsync(cancellationToken);
                    LogInfo($"初始化->[{DingSheng_Axis.Name}[在籍]回原结束]");
                    //顶升轴移动待机位
                    LogInfo($"初始化->[{DingSheng_Axis.Name}移动->[在籍][Ds_待机位]开始]");
                    await DingSheng_Axis.MoveToAsync(Ds_待机位, cancellationToken);
                    if (!DingSheng_Axis.CheckIsInPosition(Ds_待机位))
                    {
                        LogError($"初始化->[{DingSheng_Axis.Name}移动->[在籍][Ds_待机位]失败]");
                        return false;
                    }
                    LogInfo($"初始化->[{DingSheng_Axis.Name}移动->[在籍][Ds_待机位]完成]");
                }
                return true;
            }
            else
            {
                LogInfo($"初始化->[{DingSheng_Axis.Name}回原开始]");
                await DingSheng_Axis.MoveOriginAsync(cancellationToken);
                LogInfo($"初始化->[{DingSheng_Axis.Name}回原结束]");
                if (bianJuInPosition == false)//变距不在托盘位就回原
                {
                    LogInfo($"初始化->[{BianJu_Axis.Name}回原开始]");
                    await BianJu_Axis.MoveOriginAsync(cancellationToken);
                    LogInfo($"初始化->[{BianJu_Axis.Name}回原结束]");
                }
                //变距轴移动托盘位
                LogInfo($"初始化->[{BianJu_Axis.Name}移动->[Bj_托盘位]开始]");
                await BianJu_Axis.MoveToAsync(Bj_托盘位, cancellationToken);
                if (!BianJu_Axis.CheckIsInPosition(Bj_托盘位))
                {
                    LogError($"初始化->[{BianJu_Axis.Name}移动->[Bj_托盘位]失败]");
                    return false;
                }
                LogInfo($"初始化->[{BianJu_Axis.Name}移动->[Bj_托盘位]完成]");
                //顶升轴移动待机位
                LogInfo($"初始化->[{DingSheng_Axis.Name}移动->[Ds_待机位]开始]");
                await DingSheng_Axis.MoveToAsync(Ds_待机位, cancellationToken);
                if (!DingSheng_Axis.CheckIsInPosition(Ds_待机位))
                {
                    LogError($"初始化->[{DingSheng_Axis.Name}移动->[Ds_待机位]失败]");
                    return false;
                }
                LogInfo($"初始化->[{DingSheng_Axis.Name}移动->[Ds_待机位]完成]");
            }
            await PiDai.Stop();
            return true;
        }
        #endregion

        #region 运行流程
        public volatile GongLiaoRunningStatus Status;
        /// <summary>
        /// 运行步骤
        /// </summary>
        public GongLiaoStationRunStep Step
        {
            get => (GongLiaoStationRunStep)Status.Step;
            set
            {
                Status.Step = (int)value;
                Status.Save();
            }
        }
        /// <summary>
        /// 检查供料盘是否在籍
        /// </summary>
        /// <returns></returns>
        bool CheckProuductRegistered()
        {
            if (Status.HasFeedTray && 入料到位光电 && Z轴有无料光电)
            {
                return true;
            }
            else
            {
                if (prouductNotRegisteredError) return false;
                ProuductNotRegisteredError = true;
                LogError("供料盘未在籍");
                return false;
            }
        }
        /// <summary>
        /// 检查供料盘是否被清理
        /// </summary>
        /// <returns></returns>
        bool CheckProuductRegisteredClear()
        {
            if (入料到位光电 && (Cylinder_阻挡气缸1.IsBack() || Cylinder_阻挡气缸2.IsBack()))
            {
                if (prouductRegisteredClearError) return false;
                ProuductRegisteredClearError = true;
                return false;
            }
            return true;
        }
        /// <summary>
        /// 检查是否有产品进入流线当中
        /// </summary>
        bool IsProductLine => 入料口光电 || Z轴有无料光电 || 入料到位减速光电 || 出料口光电;
        bool 入料口光电 => Electronic.DI20_入料口光电.Value;
        bool Z轴有无料光电 => Electronic.DI19_入料防追尾光电.Value;
        bool 入料到位减速光电 => Electronic.DI39_入料到位减速光电.Value;
        bool 入料到位光电 => Electronic.DI21_入料到位光电.Value;
        bool 出料口光电 => Electronic.DI22_出料口光电.Value;
        GongLiaoMotionTimeOption motionTimeOption => _inspectorOption.GLMotionTimeOption;
        protected override async Task PreRunning()
        {
            try
            {
                var axisList = GetAxes();
                foreach (var axis in axisList)
                {
                    await axis.InitializeAsync();
                }
                _optionRepository = Service.GetRequiredService<IOptionRepository>();
                _inspectorOption = _optionRepository.Get<InspectorOption>();
                Option = Recipe.Get<GongLiaoStationOption>(Id);
                Status = RunningStatus.GetRecord<GongLiaoRunningStatus>("GongLiao.json");
                if (_isInitialized == false)
                {
                    throw new Exception("未初始化");
                }
                await RecoverPreRunning();
            }
            catch (Exception ex)
            {
                Root.Stop();
                LogError($"运行之前异常{ex.Message}", ex);
            }
        }
        protected override async Task RunningCoreAsync()
        {
            await Step01_阻挡气缸伸出();
            await Step02_皮带转动等待料盘到位();
            await Step03_Z轴顶升_上下气缸_顶升();
            await Step04_左右气缸_夹紧();
            await Step05_前后气缸_夹紧();
            await Step06_顶部视觉拍照_入料();
            await Step07_等待取料完成();
            await Step08_顶部视觉拍照_下料();
            await Step09_前后_左右气缸_张开();
            await Step10_Z轴顶升_上下气缸_下降();
            await Step11_皮带转动等待下料完成();
        }
        protected override async void OnStopped()
        {
            var axies = GetAxes();
            axies.ForEach(async x => await x.PauseAsync());
            await PiDai.Stop();
            PiDai.LoadYouClose();
            PiDai.UnloadYouClose();
            SaveRuningRecordState();
        }
        async Task Step01_阻挡气缸伸出()
        {
            if (Step != GongLiaoStationRunStep.Step01_阻挡气缸伸出) return;
            OnStepChanged("Step01_阻挡气缸伸出");
            LogInfo("Step01_阻挡气缸伸出->[开始]");
            if (!CheckProuductRegisteredClear())
            {
                await Task.Delay(1000);
                return;
            }
            if (Cylinder_阻挡气缸1.IsBack() || Cylinder_阻挡气缸2.IsBack())
            {
                await Task.WhenAll(
                Cylinder_阻挡气缸1.PushOutAsync2(CancellationToken, motionTimeOption.CylinderPushOutTimeOut, motionTimeOption.CylinderPushOutDelay),
                Cylinder_阻挡气缸2.PushOutAsync2(CancellationToken, motionTimeOption.CylinderPushOutTimeOut, motionTimeOption.CylinderPushOutDelay));
            }
            LogInfo("Step01_阻挡气缸伸出->[结束]");
            Step = GongLiaoStationRunStep.Step02_皮带转动等待料盘到位;
        }
        async Task Step02_皮带转动等待料盘到位()
        {
            if (Step != GongLiaoStationRunStep.Step02_皮带转动等待料盘到位) return;
            OnStepChanged("Step02_皮带转动等待料盘到位");
            LogInfo("Step02_皮带转动等待料盘到位->[开始]");
            if (DingSheng_Axis.CheckIsInPosition(Ds_待机位) == false)
            {
                throw new Exception("Step02_皮带转动等待料盘到位->[顶升Z轴未在待机位]");
            }
            if (入料到位光电 == false && 入料到位减速光电 == false)
            {
                LogInfo("Step02_皮带转动等待料盘到位->[皮带正向转]");
                _ = PiDai.PostiveRunning(CancellationToken);
                if (入料口光电 && Z轴有无料光电 == false)//表示入料盘口有料
                {
                    LogInfo("Step02_皮带转动等待料盘到位->[等待入料口光电]");
                    await Judger.Sure(() => 入料口光电 == true, CancellationToken);
                    LogInfo("Step02_皮带转动等待料盘到位->[入料口光电开]");
                    await Judger.Sure(() => 入料口光电 == false, CancellationToken);
                    LogInfo("Step02_皮带转动等待料盘到位->[入料口光电关]");
                }
                else if (入料口光电 == false && Z轴有无料光电 == false)//表示入料盘口无料
                {
                    LogInfo("Step02_皮带转动等待料盘到位->[上游请求要料开]");
                    PiDai.LoadYouOpen();
                    LogInfo("Step02_皮带转动等待料盘到位->[等待入料口光电]");
                    await Judger.Sure(() => 入料口光电 == true, CancellationToken);
                    LogInfo("Step02_皮带转动等待料盘到位->[入料口光电开]");
                    await Judger.Sure(() => 入料口光电 == false, CancellationToken);
                    LogInfo("Step02_皮带转动等待料盘到位->[入料口光电关]");
                }
                else if (入料口光电 == false && Z轴有无料光电)
                {
                    LogInfo("Step02_皮带转动等待料盘到位->[入料口光电关|Z轴有无料光电开]");
                }
                PiDai.LoadYouClose();
                LogInfo("Step02_皮带转动等待料盘到位->[上游请求要料关]");
                Status.HasFeedTray = true;
                OnStepChanged("Step02_皮带转动等待料盘到位在籍");
                LogInfo("Step02_皮带转动等待料盘到位->[等待入料到位减速光电]");
                await Judger.Sure(() => 入料到位减速光电, CancellationToken);
                LogInfo("Step02_皮带转动等待料盘到位->[入料到位减速光电开]");
                PiDai.ChangeSpeed();
                LogInfo("Step02_皮带转动等待料盘到位->[皮带减速]");
                await Judger.Sure(() => 入料到位光电, CancellationToken);
                LogInfo("Step02_皮带转动等待料盘到位->[入料到位光电开]");
                await Task.Delay(motionTimeOption.ArriveSensorDelay, CancellationToken);
                await PiDai.Stop();
                LogInfo("Step02_皮带转动等待料盘到位->[皮带停转]");
            }
            else
            {
                if (入料到位减速光电 && 入料到位光电)
                {
                    if (Cylinder_阻挡气缸1.IsBack() || Cylinder_阻挡气缸1.IsBack())
                    {
                        _ = PiDai.LowNegativeRunning(CancellationToken);
                        LogInfo("Step02_皮带转动等待料盘到位->[皮带反向转]");
                        await Judger.Sure(() => 入料到位减速光电 == false, CancellationToken);
                        LogInfo("Step02_皮带转动等待料盘到位->[入料减速光电关]");
                        await PiDai.Stop();
                        LogInfo("Step02_皮带转动等待料盘到位->[皮带停转]");
                    }
                }
                _ = PiDai.PostiveRunning(CancellationToken);
                LogInfo("Step02_皮带转动等待料盘到位->[皮带正向转]");
                await Judger.Sure(() => 入料到位减速光电 == true, CancellationToken);
                PiDai.ChangeSpeed();
                await Judger.Sure(() => 入料到位光电, CancellationToken);
                await Task.Delay(motionTimeOption.ArriveSensorDelay, CancellationToken);
                Status.HasFeedTray = true;
                OnStepChanged("Step02_皮带转动等待料盘到位在籍");
                LogInfo("Step02_皮带转动等待料盘到位->[入料到位光电开]");
                await PiDai.Stop();
                LogInfo("Step02_皮带转动等待料盘到位->[皮带停转]");
            }
            LogInfo("Step02_皮带转动等待料盘到位->[结束]");
            Step = GongLiaoStationRunStep.Step03_Z轴顶升_上下气缸_顶升;
        }
        async Task Step03_Z轴顶升_上下气缸_顶升()
        {
            if (Step != GongLiaoStationRunStep.Step03_Z轴顶升_上下气缸_顶升) return;
            OnStepChanged("Step03_Z轴顶升_上下气缸_顶升");
            LogInfo("Step03_Z轴顶升_上下气缸顶升->[开始]");
            if (!CheckProuductRegistered()) return;
            await Task.WhenAll(
                Cylinder_上下气缸1.PushOutAsync2(CancellationToken, motionTimeOption.CylinderPushOutTimeOut, motionTimeOption.CylinderPushOutDelay),
                Cylinder_上下气缸2.PushOutAsync2(CancellationToken, motionTimeOption.CylinderPushOutTimeOut, motionTimeOption.CylinderPushOutDelay),
                DingSheng_Axis.MoveToAsync(Ds_顶升位, CancellationToken));
            if (!CheckProuductRegistered()) return;
            if (!DingSheng_Axis.CheckIsInPosition(Ds_顶升位))
            {
                await Task.Delay(1000);
                throw new Exception("Step03_Z轴顶升_上下气缸顶升->[轴顶升位异常]");
            }
            if (!Cylinder_上下气缸1.IsPushOut() || !Cylinder_上下气缸2.IsPushOut())
            {
                await Task.Delay(1000);
                throw new Exception("Step03_Z轴顶升_上下气缸顶升->[气缸顶升异常]");
            }
            if (!CheckProuductRegistered()) return;
            LogInfo("Step03_Z轴顶升_上下气缸顶升->[结束]");
            Step = GongLiaoStationRunStep.Step04_左右气缸_夹紧;
        }
        async Task Step04_左右气缸_夹紧()
        {
            if (Step != GongLiaoStationRunStep.Step04_左右气缸_夹紧) return;
            OnStepChanged("Step04_左右气缸_夹紧");
            LogInfo("Step04_左右气缸_夹紧->[开始]");
            if (!CheckProuductRegistered()) return;
            await Task.WhenAll(
                Cylinder_左右气缸1.PushOutAsync2(CancellationToken, motionTimeOption.CylinderPushOutTimeOut, motionTimeOption.CylinderPushOutDelay),
                Cylinder_左右气缸2.PushOutAsync2(CancellationToken, motionTimeOption.CylinderPushOutTimeOut, motionTimeOption.CylinderPushOutDelay));
            if (!CheckProuductRegistered()) return;
            LogInfo("Step04_左右气缸_夹紧->[结束]");
            Step = GongLiaoStationRunStep.Step05_前后气缸_夹紧;
        }
        async Task Step05_前后气缸_夹紧()
        {
            if (Step != GongLiaoStationRunStep.Step05_前后气缸_夹紧) return;
            OnStepChanged("Step05_前后气缸_夹紧");
            LogInfo("Step05_前后气缸_夹紧->[开始]");
            if (!CheckProuductRegistered()) return;
            await Task.WhenAll(
                Cylinder_前后气缸1.PushOutAsync2(CancellationToken, motionTimeOption.CylinderPushOutTimeOut, motionTimeOption.CylinderPushOutDelay),
                Cylinder_前后气缸2.PushOutAsync2(CancellationToken, motionTimeOption.CylinderPushOutTimeOut, motionTimeOption.CylinderPushOutDelay));
            if (!CheckProuductRegistered()) return;
            LogInfo("Step05_前后气缸_夹紧->[结束]");
            _calcTimeService.CalcCT();//计算CT
            Step = GongLiaoStationRunStep.Step06_顶部视觉拍照_入料;
        }
        async Task Step06_顶部视觉拍照_入料()
        {
            if (Step != GongLiaoStationRunStep.Step06_顶部视觉拍照_入料) return;
            OnStepChanged("Step06_顶部视觉拍照_入料");
            LogInfo("Step06_顶部视觉拍照_入料->[开始]");
            if (!CheckProuductRegistered()) return;
            var result = await Vision.TopCapture();
            if (result.Quality != Quality.Ok)
            {
                LogError("Step06_顶部视觉拍照_入料->[拍照NG]");
                FeedTrayLoadError = true;
                await Task.Delay(1000);
                return;
            }
            if (!CheckProuductRegistered()) return;
            FeedTray.Instance.SetTrayItem(result.HasProducts);
            FeedTray.Instance.Save();
            SignalPanel.Signal.FeedTrayOffset = result.Offset;
            SignalPanel.Signal.CurrentFeedTray = result.Barcode;
            SignalPanel.Signal.FeedTrayReady = true;
            LogInfo("Step06_顶部视觉拍照_入料->[结束]");
            Step = GongLiaoStationRunStep.Step07_等待取料完成;
        }
        async Task Step07_等待取料完成()
        {
            if (Step != GongLiaoStationRunStep.Step07_等待取料完成) return;
            OnStepChanged("Step07_等待取料完成");
            LogInfo("Step07_等待取料完成->[开始]");
            if (!CheckProuductRegistered()) return;
            await Judger.Sure(() => SignalPanel.Signal.FeedTrayCompleted, CancellationToken);
            if (!CheckProuductRegistered()) return;
            SignalPanel.Signal.FeedTrayReady = false;
            SignalPanel.Signal.FeedTrayCompleted = false;
            LogInfo("Step07_等待取料完成->[结束]");
            Step = GongLiaoStationRunStep.Step08_顶部视觉拍照_下料;//Step = GongLiaoStationRunStep.Step09_前后_左右气缸_张开;
        }
        async Task Step08_顶部视觉拍照_下料()
        {
            if (Step != GongLiaoStationRunStep.Step08_顶部视觉拍照_下料) return;
            OnStepChanged("Step08_顶部视觉拍照_下料");
            LogInfo("Step08_顶部视觉拍照_下料->[开始]");
            if (!CheckProuductRegistered()) return;
            var result = await Vision.TopCapture(true);
            if (result.Quality != Quality.Ok)
            {
                LogError("Step08_顶部视觉拍照_下料->[视觉拍照失败]");
                FeedTrayUnloadError = true;
                await Task.Delay(1000);
                return;
            }
            var hasProducts = result.HasProducts;
            var feedTrayColunms = FeedTray.Instance.FeedTrayColunms;
            int i = 0;
            foreach (var colunm in feedTrayColunms)//用于判断那个是否料盘料是否有掉料防反
            {
                foreach (var item in colunm.Items)
                {
                    if (item.Status != TrayPointStatus.Empty)
                    {
                        var hasProduct = hasProducts[i];
                        if (!hasProduct)
                        {
                            FeedTrayUnloadError = true;
                            LogError("Step08_顶部视觉拍照_下料->[视觉拍照失败]");
                            await Task.Delay(1000);
                            return;
                        }
                    }
                    i++;
                }
            }
            LogInfo("Step08_顶部视觉拍照_下料->[结束]");
            Step = GongLiaoStationRunStep.Step09_前后_左右气缸_张开;
        }
        async Task Step09_前后_左右气缸_张开()
        {
            if (Step != GongLiaoStationRunStep.Step09_前后_左右气缸_张开) return;
            OnStepChanged("Step09_前后_左右气缸_张开");
            LogInfo("Step09_前后_左右气缸_张开->[开始]");
            if (!CheckProuductRegistered()) return;
            await Task.WhenAll(
                Cylinder_前后气缸1.TakeBackAsync2(CancellationToken, motionTimeOption.CylinderTakeBackTimeOut, motionTimeOption.CylinderTakeBackDelay),
                Cylinder_前后气缸2.TakeBackAsync2(CancellationToken, motionTimeOption.CylinderTakeBackTimeOut, motionTimeOption.CylinderTakeBackDelay),
                Cylinder_左右气缸1.TakeBackAsync2(CancellationToken, motionTimeOption.CylinderTakeBackTimeOut, motionTimeOption.CylinderTakeBackDelay),
                Cylinder_左右气缸2.TakeBackAsync2(CancellationToken, motionTimeOption.CylinderTakeBackTimeOut, motionTimeOption.CylinderTakeBackDelay));
            if (!CheckProuductRegistered()) return;
            LogInfo("Step09_前后_左右气缸_张开->[结束]");
            Step = GongLiaoStationRunStep.Step10_Z轴顶升_上下气缸_下降;
        }
        async Task Step10_Z轴顶升_上下气缸_下降()
        {
            if (Step != GongLiaoStationRunStep.Step10_Z轴顶升_上下气缸_下降) return;
            OnStepChanged("Step10_Z轴顶升_上下气缸_下降");
            LogInfo("Step10_Z轴顶升_上下气缸_下降->[开始]");
            if (!CheckProuductRegistered()) return;
            await Task.WhenAll(
                Cylinder_阻挡气缸1.TakeBackAsync2(CancellationToken, motionTimeOption.CylinderTakeBackTimeOut, motionTimeOption.CylinderTakeBackDelay),
                Cylinder_阻挡气缸2.TakeBackAsync2(CancellationToken, motionTimeOption.CylinderTakeBackTimeOut, motionTimeOption.CylinderTakeBackDelay),
                Cylinder_上下气缸1.TakeBackAsync2(CancellationToken, motionTimeOption.CylinderTakeBackTimeOut, motionTimeOption.CylinderTakeBackDelay),
                Cylinder_上下气缸2.TakeBackAsync2(CancellationToken, motionTimeOption.CylinderTakeBackTimeOut, motionTimeOption.CylinderTakeBackDelay),
                DingSheng_Axis.MoveToAsync(Ds_待机位, CancellationToken));
            if (!CheckProuductRegistered()) return;
            if (!DingSheng_Axis.CheckIsInPosition(Ds_待机位))
            {
                await Task.Delay(1000);
                throw new Exception("Step10_Z轴顶升_上下气缸_下降->[待机位异常]");
            }
            if (!Cylinder_上下气缸1.IsBack() || !Cylinder_上下气缸2.IsBack())
            {
                await Task.Delay(1000);
                throw new Exception("Step10_Z轴顶升_上下气缸_下降->[气缸下降异常]");
            }
            if (!CheckProuductRegistered()) return;
            LogInfo("Step10_Z轴顶升_上下气缸_下降->[结束]");
            Step = GongLiaoStationRunStep.Step11_皮带转动等待下料完成;
        }
        async Task Step11_皮带转动等待下料完成()
        {
            if (Step != GongLiaoStationRunStep.Step11_皮带转动等待下料完成) return;
            OnStepChanged("Step11_皮带转动等待下料完成");
            LogInfo("Step11_皮带转动等待下料完成->[开始]");
            await Judger.Sure(() => Electronic.DI95_下料交互输入信号1模组继电器.Value, CancellationToken);
            LogInfo("Step11_皮带转动等待下料完成->[皮带转动]");
            if (Z轴有无料光电 && 入料到位减速光电 && 出料口光电)
            {
                CheckProuductJoin("Step11_皮带转动等待下料完成");
            }
            if (入料口光电)//如果入料口有料，就采取低速模式
            {
                _ = PiDai.LowPostiveRunning(CancellationToken);
            }
            else //如果入料口无料，就采取高速模式
            {
                _ = PiDai.PostiveRunning(CancellationToken);
            }
            if (入料到位光电)
            {
                await Judger.Sure(() => 入料到位光电, CancellationToken);
                LogInfo("Step11_皮带转动等待下料完成->[入料光电开]");
                await Judger.Sure(() => !入料到位光电, CancellationToken);
                LogInfo("Step11_皮带转动等待下料完成->[入料光电关]");
            }
            if (入料口光电 || Z轴有无料光电 || 入料到位减速光电)
            {

            }
            else
            {
                PiDai.LoadYouOpen();
                LogInfo("Step11_皮带转动等待下料完成->[上游请求要料]");
            }
            if (出料口光电)
            {
                await Judger.Sure(() => 出料口光电, CancellationToken);
                LogInfo("Step11_皮带转动等待下料完成->[出口光电开]");
                await Judger.Sure(() => CheckProuductJoin("Step11_皮带转动等待下料完成") && !出料口光电, CancellationToken);
                LogInfo("Step11_皮带转动等待下料完成->[出口光电关]");
            }
            await Judger.Sure(() => Electronic.DI95_下料交互输入信号1模组继电器.Value == false, CancellationToken);
            LogInfo("Step11_皮带转动等待下料完成->[结束]");
            Step = GongLiaoStationRunStep.Step01_阻挡气缸伸出;
            Status.HasFeedTray = false;
        }

        bool CheckProuductJoin(string stepstring)
        {
            if ((入料到位减速光电 || 入料到位光电))
            {
                if (prouductJoinError) return false;
                ProuductJoinError = true;
                LogError($"{stepstring}->[供料盘连扳]");
                return false;
            }
            return true;
        }

        async Task Setp12_过站_前后气缸张开()
        {
            if (Step != GongLiaoStationRunStep.Setp12_过站_前后气缸张开) return;
            OnStepChanged("Setp12_过站_前后气缸张开");
            LogInfo("Setp12_过站_前后气缸张开->[开始]");
            await Task.WhenAll(
                Cylinder_前后气缸1.TakeBackAsync2(CancellationToken, motionTimeOption.CylinderTakeBackTimeOut, motionTimeOption.CylinderTakeBackDelay),
                Cylinder_前后气缸2.TakeBackAsync2(CancellationToken, motionTimeOption.CylinderTakeBackTimeOut, motionTimeOption.CylinderTakeBackDelay));
            LogInfo("Setp12_过站_前后气缸张开->[结束]");
            Step = GongLiaoStationRunStep.Setp13_过站_上下气缸_Z轴_阻挡下降;
        }
        async Task Setp13_过站_上下气缸_Z轴_阻挡下降()
        {
            if (Step != GongLiaoStationRunStep.Setp13_过站_上下气缸_Z轴_阻挡下降) return;
            OnStepChanged("Setp13_过站_上下气缸_Z轴_阻挡下降");
            LogInfo("Setp13_过站_上下气缸_Z轴_阻挡下降->[开始]");
            await Task.WhenAll(
              Cylinder_上下气缸1.TakeBackAsync2(CancellationToken, motionTimeOption.CylinderTakeBackTimeOut, motionTimeOption.CylinderTakeBackDelay),
              Cylinder_上下气缸2.TakeBackAsync2(CancellationToken, motionTimeOption.CylinderTakeBackTimeOut, motionTimeOption.CylinderTakeBackDelay),
              Cylinder_阻挡气缸1.TakeBackAsync2(CancellationToken, motionTimeOption.CylinderTakeBackTimeOut, motionTimeOption.CylinderTakeBackDelay),
              Cylinder_阻挡气缸2.TakeBackAsync2(CancellationToken, motionTimeOption.CylinderTakeBackTimeOut, motionTimeOption.CylinderTakeBackDelay),
             DingSheng_Axis.MoveToAsync(Ds_待机位));
            LogInfo("Setp13_过站_上下气缸_Z轴_阻挡下降->[结束]");
            Step = GongLiaoStationRunStep.Setp14_过站_等待过站完成;

        }
        async Task Setp14_过站_等待过站完成()
        {
            if (Step != GongLiaoStationRunStep.Setp14_过站_等待过站完成) return;
            OnStepChanged("Setp14_过站_等待过站完成");
            LogInfo("Setp14_过站_等待过站完成->[开始]");
            Electronic.Default.DI96_下料交互输入信号2模组继电器.Value = true;
            await Judger.Sure(() => Electronic.Default.DI96_下料交互输入信号2模组继电器.Value, CancellationToken);
            Electronic.Default.DI96_下料交互输入信号2模组继电器.Value = false;
            await Judger.Sure(() => Electronic.Default.DI96_下料交互输入信号2模组继电器.Value == false, CancellationToken);
            LogInfo("Setp14_过站_等待过站完成->[结束]");
            Step = GongLiaoStationRunStep.Setp15_过站_上下气缸_Z轴上升;

        }
        async Task Setp15_过站_上下气缸_Z轴上升()
        {
            if (Step != GongLiaoStationRunStep.Setp16_过站_阻挡气缸伸出) return;
            OnStepChanged("Setp15_过站_上下气缸_Z轴上升");
            LogInfo("Setp15_过站_上下气缸_Z轴上升->[开始]");
            await Task.WhenAll(
            Cylinder_上下气缸1.PushOutAsync2(CancellationToken, motionTimeOption.CylinderPushOutTimeOut, motionTimeOption.CylinderPushOutDelay),
            Cylinder_上下气缸2.PushOutAsync2(CancellationToken, motionTimeOption.CylinderPushOutTimeOut, motionTimeOption.CylinderPushOutDelay),
            DingSheng_Axis.MoveToAsync(Ds_顶升位));
            LogInfo("Setp15_过站_上下气缸_Z轴上升->[结束]");
            Step = GongLiaoStationRunStep.Setp16_过站_阻挡气缸伸出;

        }
        async Task Setp16_过站_阻挡气缸伸出()
        {
            if (Step != GongLiaoStationRunStep.Setp16_过站_阻挡气缸伸出) return;
            OnStepChanged("Setp16_过站_阻挡气缸伸出");
            LogInfo("Setp16_过站_阻挡气缸伸出->[开始]");
            await Task.WhenAll(
            Cylinder_阻挡气缸1.PushOutAsync2(CancellationToken, motionTimeOption.CylinderPushOutTimeOut, motionTimeOption.CylinderPushOutDelay),
            Cylinder_阻挡气缸1.PushOutAsync2(CancellationToken, motionTimeOption.CylinderPushOutTimeOut, motionTimeOption.CylinderPushOutDelay),
            DingSheng_Axis.MoveToAsync(Ds_顶升位));
            LogInfo("Setp16_过站_阻挡气缸伸出->[结束]");
            Step = GongLiaoStationRunStep.Setp17_过站_前后气缸夹紧;
        }
        async Task Setp17_过站_前后气缸夹紧()
        {
            if (Step != GongLiaoStationRunStep.Setp17_过站_前后气缸夹紧) return;
            OnStepChanged("Setp17_过站_前后气缸夹紧");
            LogInfo("Setp17_过站_前后气缸夹紧->[开始]");
            await Task.WhenAll(
            Cylinder_前后气缸1.PushOutAsync2(CancellationToken, motionTimeOption.CylinderPushOutTimeOut, motionTimeOption.CylinderPushOutDelay),
            Cylinder_前后气缸2.PushOutAsync2(CancellationToken, motionTimeOption.CylinderPushOutTimeOut, motionTimeOption.CylinderPushOutDelay),
            DingSheng_Axis.MoveToAsync(Ds_顶升位));
            LogInfo("Setp17_过站_前后气缸夹紧->[结束]");
            Step = GongLiaoStationRunStep.Step07_等待取料完成;
        }
        #endregion

        #region 状态恢复流程

        GongLiaoRunningStatus _preRunStatus;
        /// <summary>
        /// 状态恢复流程异常
        /// </summary>
        bool _isRecoverPreRunningError = false;
        /// <summary>
        /// 状态恢复标志位
        /// </summary>
        public bool RecoverPreStateFlag { get; private set; }
        /// <summary>
        /// 复到到停止运行之前检测状态
        /// </summary>
        /// <returns></returns>
        protected async Task RecoverPreRunning()
        {
            try
            {
                LogInfo($"运行状态恢复->[开始]");
                RecoverPreStateFlag = false;
                _isRecoverPreRunningError = false;
                _preRunStatus = Status.Clone();
                var isProuductRegistered = IsProuductRegistered;
                if (isProuductRegistered) //产品在籍恢复之前状态
                {
                    if (Step == GongLiaoStationRunStep.Step02_皮带转动等待料盘到位)
                    {
                        if (!DingSheng_Axis.CheckIsInPosition(Status.DSAxisPositions))//恢复顶升轴
                        {
                            await DingSheng_Axis.MoveToAsync(Status.DSAxisPositions);
                        }
                        //阻挡气缸恢复
                        await ManualCheckAsync(Cylinder_阻挡气缸1, Status.CylinderResist1);
                        await ManualCheckAsync(Cylinder_阻挡气缸2, Status.CylinderResist2);
                        //左右气缸恢复
                        if (!Cylinder_左右气缸1.VerifyState(Status.CylinderLeftRight1))
                            await Cylinder_左右气缸1.RecoveryState(Status.CylinderLeftRight1, CancellationToken);
                        if (!Cylinder_左右气缸2.VerifyState(Status.CylinderLeftRight2))
                            await Cylinder_左右气缸2.RecoveryState(Status.CylinderLeftRight2, CancellationToken);
                        //上下前后气缸恢复
                        await RecoverTopBotFrontBackCylinder(Cylinder_上下气缸1, Cylinder_前后气缸1, Status.CylinderTopBot1, Status.CylinderFront1);
                        await RecoverTopBotFrontBackCylinder(Cylinder_上下气缸2, Cylinder_前后气缸2, Status.CylinderTopBot2, Status.CylinderFront2);
                    }
                    else if (Step == GongLiaoStationRunStep.Step03_Z轴顶升_上下气缸_顶升)
                    {
                        //阻挡气缸恢复
                        await ManualCheckAsync(Cylinder_阻挡气缸1, Status.CylinderResist1);
                        await ManualCheckAsync(Cylinder_阻挡气缸2, Status.CylinderResist2);
                        //左右气缸恢复
                        if (!Cylinder_左右气缸1.VerifyState(Status.CylinderLeftRight1))
                            await Cylinder_左右气缸1.RecoveryState(Status.CylinderLeftRight1, CancellationToken);
                        if (!Cylinder_左右气缸2.VerifyState(Status.CylinderLeftRight2))
                            await Cylinder_左右气缸2.RecoveryState(Status.CylinderLeftRight2, CancellationToken);
                        //上下前后气缸恢复
                        await RecoverTopBotFrontBackCylinder(Cylinder_上下气缸1, Cylinder_前后气缸1, Status.CylinderTopBot1, Status.CylinderFront1);
                        await RecoverTopBotFrontBackCylinder(Cylinder_上下气缸2, Cylinder_前后气缸2, Status.CylinderTopBot2, Status.CylinderFront2);
                    }
                    else if (Step == GongLiaoStationRunStep.Step04_左右气缸_夹紧 || Step == GongLiaoStationRunStep.Step05_前后气缸_夹紧)
                    {
                        if (!DingSheng_Axis.CheckIsInPosition(Status.DSAxisPositions))//恢复顶升轴
                        {
                            await DingSheng_Axis.MoveToAsync(Status.DSAxisPositions);
                        }
                        //阻挡气缸恢复
                        await ManualCheckAsync(Cylinder_阻挡气缸1, Status.CylinderResist1);
                        await ManualCheckAsync(Cylinder_阻挡气缸2, Status.CylinderResist2);
                        if (Step == GongLiaoStationRunStep.Step05_前后气缸_夹紧)
                        {
                            //左右气缸恢复
                            if (!Cylinder_左右气缸1.VerifyState(Status.CylinderLeftRight1))
                                await Cylinder_左右气缸1.RecoveryState(Status.CylinderLeftRight1, CancellationToken);
                            if (!Cylinder_左右气缸2.VerifyState(Status.CylinderLeftRight2))
                                await Cylinder_左右气缸2.RecoveryState(Status.CylinderLeftRight2, CancellationToken);
                        }
                        //上下前后气缸恢复
                        await RecoverTopBotFrontBackCylinder(Cylinder_上下气缸1, Cylinder_前后气缸1, Status.CylinderTopBot1, Status.CylinderFront1);
                        await RecoverTopBotFrontBackCylinder(Cylinder_上下气缸2, Cylinder_前后气缸2, Status.CylinderTopBot2, Status.CylinderFront2);
                    }
                    if (Step == GongLiaoStationRunStep.Step06_顶部视觉拍照_入料 || Step == GongLiaoStationRunStep.Step07_等待取料完成 || Step == GongLiaoStationRunStep.Step08_顶部视觉拍照_下料)
                    {
                        if (!DingSheng_Axis.CheckIsInPosition(Status.DSAxisPositions))//恢复顶升轴
                        {
                            await DingSheng_Axis.MoveToAsync(Status.DSAxisPositions);
                        }
                        if (!BianJu_Axis.CheckIsInPosition(Status.BJAxisPositions))//恢复变距轴
                        {
                            await BianJu_Axis.MoveToAsync(Status.BJAxisPositions);
                        }
                        //阻挡气缸恢复
                        await ManualCheckAsync(Cylinder_阻挡气缸1, Status.CylinderResist1);
                        await ManualCheckAsync(Cylinder_阻挡气缸2, Status.CylinderResist2);
                        //左右气缸恢复
                        if (!Cylinder_左右气缸1.VerifyState(Status.CylinderLeftRight1))
                            await Cylinder_左右气缸1.RecoveryState(Status.CylinderLeftRight1, CancellationToken);
                        if (!Cylinder_左右气缸2.VerifyState(Status.CylinderLeftRight2))
                            await Cylinder_左右气缸2.RecoveryState(Status.CylinderLeftRight2, CancellationToken);
                        //上下前后气缸恢复
                        await RecoverTopBotFrontBackCylinder(Cylinder_上下气缸1, Cylinder_前后气缸1, Status.CylinderTopBot1, Status.CylinderFront1);
                        await RecoverTopBotFrontBackCylinder(Cylinder_上下气缸2, Cylinder_前后气缸2, Status.CylinderTopBot2, Status.CylinderFront2);
                    }
                    else if (Step == GongLiaoStationRunStep.Step10_Z轴顶升_上下气缸_下降)
                    {
                        //阻挡气缸恢复
                        await ManualCheckAsync(Cylinder_阻挡气缸1, Status.CylinderResist1);
                        await ManualCheckAsync(Cylinder_阻挡气缸2, Status.CylinderResist2);
                        //上下前后气缸恢复
                        await RecoverTopBotFrontBackCylinder(Cylinder_上下气缸1, Cylinder_前后气缸1, Status.CylinderTopBot1, Status.CylinderFront1);
                        await RecoverTopBotFrontBackCylinder(Cylinder_上下气缸2, Cylinder_前后气缸2, Status.CylinderTopBot2, Status.CylinderFront2);
                        //左右气缸恢复
                        if (!Cylinder_左右气缸1.VerifyState(Status.CylinderLeftRight1))
                            await Cylinder_左右气缸1.RecoveryState(Status.CylinderLeftRight1, CancellationToken);
                        if (!Cylinder_左右气缸2.VerifyState(Status.CylinderLeftRight2))
                            await Cylinder_左右气缸2.RecoveryState(Status.CylinderLeftRight2, CancellationToken);
                    }
                    else if (Step == GongLiaoStationRunStep.Step11_皮带转动等待下料完成)
                    {
                        if (!DingSheng_Axis.CheckIsInPosition(Status.DSAxisPositions))//恢复顶升轴
                        {
                            await DingSheng_Axis.MoveToAsync(Status.DSAxisPositions);
                        }
                        //阻挡气缸恢复
                        await ManualCheckAsync(Cylinder_阻挡气缸1, Status.CylinderResist1);
                        await ManualCheckAsync(Cylinder_阻挡气缸2, Status.CylinderResist2);
                        //上下前后气缸恢复
                        await RecoverTopBotFrontBackCylinder(Cylinder_上下气缸1, Cylinder_前后气缸1, Status.CylinderTopBot1, Status.CylinderFront1);
                        await RecoverTopBotFrontBackCylinder(Cylinder_上下气缸2, Cylinder_前后气缸2, Status.CylinderTopBot2, Status.CylinderFront2);
                        //左右气缸恢复
                        if (!Cylinder_左右气缸1.VerifyState(Status.CylinderLeftRight1))
                            await Cylinder_左右气缸1.RecoveryState(Status.CylinderLeftRight1, CancellationToken);
                        if (!Cylinder_左右气缸2.VerifyState(Status.CylinderLeftRight2))
                            await Cylinder_左右气缸2.RecoveryState(Status.CylinderLeftRight2, CancellationToken);
                    }
                }
                else //产品未在籍，初始化回到第一步
                {
                    if (Step == GongLiaoStationRunStep.Step01_阻挡气缸伸出)
                    {
                        if (!DingSheng_Axis.CheckIsInPosition(Ds_待机位))
                            await DingSheng_Axis.MoveToAsync(Ds_待机位);
                        if (Cylinder_前后气缸1.IsPushOut() || Cylinder_前后气缸2.IsPushOut())
                        {
                            await Task.WhenAll(
                                 Cylinder_前后气缸1.TakeBackAsync2(CancellationToken, motionTimeOption.CylinderTakeBackTimeOut, motionTimeOption.CylinderTakeBackDelay),
                                 Cylinder_前后气缸2.TakeBackAsync2(CancellationToken, motionTimeOption.CylinderTakeBackTimeOut, motionTimeOption.CylinderTakeBackDelay));
                        }
                        if (Cylinder_上下气缸1.IsPushOut() || Cylinder_上下气缸2.IsPushOut())
                        {
                            await Task.WhenAll(
                                 Cylinder_上下气缸1.TakeBackAsync2(CancellationToken, motionTimeOption.CylinderTakeBackTimeOut, motionTimeOption.CylinderTakeBackDelay),
                                 Cylinder_上下气缸2.TakeBackAsync2(CancellationToken, motionTimeOption.CylinderTakeBackTimeOut, motionTimeOption.CylinderTakeBackDelay));
                        }
                        if (入料到位光电 == false && 入料到位减速光电 == false)//没有感应到产品
                        {

                        }
                        else
                        {
                            if (Cylinder_阻挡气缸1.IsBack() || Cylinder_阻挡气缸2.IsBack())
                            {
                                _ = PiDai.LowNegativeRunning(CancellationToken);
                                await Judger.Sure(() => 入料到位减速光电 == false, CancellationToken);
                                await PiDai.Stop();
                                if (入料到位光电 == false && 出料口光电 == false)//没有感应到产品
                                {
                                    if (Cylinder_阻挡气缸1.IsBack())
                                        await Cylinder_阻挡气缸1.PushOutAsync(token: CancellationToken);
                                    if (Cylinder_阻挡气缸2.IsBack())
                                        await Cylinder_阻挡气缸2.PushOutAsync(token: CancellationToken);
                                }
                            }
                        }
                    }
                    else if (Step == GongLiaoStationRunStep.Step02_皮带转动等待料盘到位)
                    {
                        if (!BianJu_Axis.CheckIsInPosition(Bj_托盘位))
                            await BianJu_Axis.MoveToAsync(Bj_托盘位);
                        if (!DingSheng_Axis.CheckIsInPosition(Ds_待机位))
                            await DingSheng_Axis.MoveToAsync(Ds_待机位);
                        //阻挡气缸恢复
                        await ManualCheckAsync(Cylinder_阻挡气缸1, Status.CylinderResist1);
                        await ManualCheckAsync(Cylinder_阻挡气缸2, Status.CylinderResist2);
                        //左右气缸恢复
                        if (!Cylinder_左右气缸1.VerifyState(Status.CylinderLeftRight1))
                            await Cylinder_左右气缸1.RecoveryState(Status.CylinderLeftRight1, CancellationToken);
                        if (!Cylinder_左右气缸2.VerifyState(Status.CylinderLeftRight2))
                            await Cylinder_左右气缸2.RecoveryState(Status.CylinderLeftRight2, CancellationToken);
                        //上下前后气缸恢复
                        await RecoverTopBotFrontBackCylinder(Cylinder_上下气缸1, Cylinder_前后气缸1, Status.CylinderTopBot1, Status.CylinderFront1);
                        await RecoverTopBotFrontBackCylinder(Cylinder_上下气缸2, Cylinder_前后气缸2, Status.CylinderTopBot2, Status.CylinderFront2);
                    }
                    else if (Step == GongLiaoStationRunStep.Step11_皮带转动等待下料完成)
                    {

                    }
                    else
                    {
                        if (Status.HasFeedTray)//产品有料在籍，但是传感应无料则报警
                        {
                            if (!CheckProuductRegistered())
                            {
                                _isRecoverPreRunningError = false;
                                return;
                            }
                        }
                        var init = await InitializeStation(CancellationToken);
                        if (init)
                            Step = GongLiaoStationRunStep.Step01_阻挡气缸伸出;
                    }
                }
                LogInfo($"运行状态恢复->[结束]");
                RecoverPreStateFlag = true;
            }
            catch (Exception ex)
            {
                _isRecoverPreRunningError = true;
                RecoverPreStateFlag = false;
                Root.Stop();
                LogError($"运行状态恢复->[异常]{ex.Message}", ex);
            }
        }
        async Task RecoverTopBotFrontBackCylinder(CylinderEx cylinderTopBot, CylinderEx cylinderFrontBack, CylinderStates cylinderTopBotState, CylinderStates cylinderFrontBackState)
        {
            if (cylinderTopBot.VerifyState(cylinderTopBotState) == false)
            {
                if (cylinderTopBotState == CylinderStates.PushOut)
                {
                    if (cylinderFrontBack.IsPushOut())//判断前后气缸是否动位,先缩回原位
                        await cylinderFrontBack.TakeBackAsync();
                    await cylinderTopBot.PushOutAsync();
                    if (cylinderFrontBackState == CylinderStates.PushOut)
                    {
                        await cylinderFrontBack.PushOutAsync();
                    }
                    else
                    {
                        await cylinderFrontBack.TakeBackAsync();
                    }
                }
                else
                {
                    if (cylinderFrontBack.IsPushOut())
                        await cylinderFrontBack.TakeBackAsync();
                    await cylinderTopBot.TakeBackAsync();
                    if (cylinderFrontBackState == CylinderStates.PushOut)
                    {
                        await cylinderFrontBack.PushOutAsync();
                    }
                    else
                    {
                        await cylinderFrontBack.TakeBackAsync();
                    }
                }
            }
            else
            {
                if (cylinderFrontBack.VerifyState(cylinderFrontBackState) == false)
                {
                    if (cylinderFrontBackState == CylinderStates.PushOut)
                    {
                        if (cylinderTopBot.IsPushOut())
                        {
                            await cylinderFrontBack.PushOutAsync();
                        }
                    }
                    else
                    {
                        if (cylinderTopBot.IsPushOut())
                        {
                            await cylinderFrontBack.TakeBackAsync();
                        }
                    }
                }
            }
        }
        async Task ManualCheckAsync(CylinderEx cylinder, CylinderStates cylinderState)
        {
            var msg = cylinderState == CylinderStates.PushOut ? "推出" : "收回";
            if (!cylinder.VerifyState(cylinderState))
            {
                var state = await OnManualStepsConfirm($"请确认[{cylinder.Name}]{msg}是否干涉?");
                if (state)
                {
                    await cylinder.RecoveryState(cylinderState, CancellationToken);
                }
                else
                {
                    throw new InvalidOperationException($"人工未确认[{cylinder.Name}]{msg}是否干涉!");
                }
            }
        }
        #endregion

        #region 公共方法
        public void OnStepChanged(string step)
        {
            StepsChanged?.SafeInvoke(this, step);
        }
        public async Task<bool> OnManualStepsConfirm(string message)
        {
            return await ManualStepsConfirmChanged.Invoke(message);
        }

        public override async Task InitializeAsync()
        {
            _calcTimeService = Service.GetRequiredService<ICalcTimeService>();
            _optionRepository = Service.GetRequiredService<IOptionRepository>();
            _inspectorOption = _optionRepository.Get<InspectorOption>();
            Status = RunningStatus.GetRecord<GongLiaoRunningStatus>("GongLiao.json");
            Logger = Service.GetService<ILoggerFactory>().CreateLogger("GongLiaoStation");
            await base.InitializeAsync();
        }
        public void ClearAlarm()
        {
            FeedTrayLoadError = false;
            FeedTrayUnloadError = false;
            ProuductNotRegisteredError = false;
            ProuductRegisteredClearError = false;
            ProuductJoinError = false;
        }
        public void Refreshing()
        {
            // 暂无实现
        }
        protected IEnumerable<DmcAxis> GetAxes()
        {
            return Automatics.Where(x => x is DmcAxis).Cast<DmcAxis>().ToArray();
        }
        #endregion

        #region 私有方法
        /// <summary>
        /// 检查令牌是否被取消
        /// </summary>
        /// <returns></returns>
        private async Task CheckCancellationToken()
        {
            await Task.Delay(1, CancellationToken);
        }
        void SaveRuningRecordState()
        {
            if (_isInitialized)
            {
                #region 阻挡气缸
                if (Cylinder_阻挡气缸1.IsPushOut())
                    Status.CylinderResist1 = CylinderStates.PushOut;
                if (Cylinder_阻挡气缸1.IsBack())
                    Status.CylinderResist1 = CylinderStates.TakeBack;

                if (Cylinder_阻挡气缸2.IsPushOut())
                    Status.CylinderResist2 = CylinderStates.PushOut;
                if (Cylinder_阻挡气缸2.IsBack())
                    Status.CylinderResist2 = CylinderStates.TakeBack;
                #endregion

                #region 前后气缸
                if (Cylinder_前后气缸1.IsPushOut())
                    Status.CylinderFront1 = CylinderStates.PushOut;
                if (Cylinder_前后气缸1.IsBack())
                    Status.CylinderFront1 = CylinderStates.TakeBack;

                if (Cylinder_前后气缸2.IsPushOut())
                    Status.CylinderFront2 = CylinderStates.PushOut;
                if (Cylinder_前后气缸2.IsBack())
                    Status.CylinderFront2 = CylinderStates.TakeBack;
                #endregion

                #region 上下气缸
                if (Cylinder_上下气缸1.IsPushOut())
                    Status.CylinderTopBot1 = CylinderStates.PushOut;
                if (Cylinder_上下气缸1.IsBack())
                    Status.CylinderTopBot1 = CylinderStates.TakeBack;

                if (Cylinder_上下气缸2.IsPushOut())
                    Status.CylinderTopBot2 = CylinderStates.PushOut;
                if (Cylinder_上下气缸2.IsBack())
                    Status.CylinderTopBot2 = CylinderStates.TakeBack;
                #endregion

                #region 左右气缸
                if (Cylinder_左右气缸1.IsPushOut())
                    Status.CylinderLeftRight1 = CylinderStates.PushOut;
                if (Cylinder_左右气缸1.IsBack())
                    Status.CylinderLeftRight1 = CylinderStates.TakeBack;

                if (Cylinder_左右气缸2.IsPushOut())
                    Status.CylinderLeftRight2 = CylinderStates.PushOut;
                if (Cylinder_左右气缸2.IsBack())
                    Status.CylinderLeftRight2 = CylinderStates.TakeBack;
                #endregion

                #region 轴点位
                Status.BJAxisPositions = BianJu_Axis.Current;
                Status.DSAxisPositions = DingSheng_Axis.Current;
                #endregion
            }
            #region 如果发生异常把之前状态赋值回去
            if (_isRecoverPreRunningError && _preRunStatus != null)
            {
                Status = _preRunStatus;
            }
            #endregion
            Status.Save();
        }
        //private void SaveRuningRecordStateInit()
        //{
        //    Status.BJAxisPositions = BianJu_Axis.Current;
        //    Status.DSAxisPositions = DingSheng_Axis.Current;
        //    Status.Save();
        //}
        #endregion

        #region 防呆方法
        /// <summary>
        /// 上下气缸防呆逻辑
        /// </summary>
        /// <returns></returns>
        bool IsFoolProofingLoadTopBotCylinder()
        {
            if (入料口光电)
            {
                return false;
            }
            else if (入料口光电 && 入料到位光电)
            {
                return false;
            }
            else if (Z轴有无料光电 && 入料到位减速光电 && 入料到位光电 == false)
            {
                return false;
            }
            else if (Electronic.DI32_入料前后拍板气缸1伸出磁开.Value || Electronic.DI34_入料前后拍板气缸2伸出磁开.Value)
            {
                return false;
            }
            else
            {
                return true;
            }
        }
        /// <summary>
        /// 顶升Z轴防呆逻辑
        /// </summary>
        /// <returns></returns>
        bool IsFoolProofingJackingAxis()
        {
            if (入料口光电)
            {
                return false;
            }
            else if (入料口光电 && 入料到位光电)
            {
                return false;
            }
            else if (Z轴有无料光电 && 入料到位减速光电 && 入料到位光电 == false)
            {
                return false;
            }
            else if ((Electronic.DI32_入料前后拍板气缸1伸出磁开.Value == false || Electronic.DI34_入料前后拍板气缸2伸出磁开.Value == false) && 出料口光电)
            {
                return false;
            }
            else
            {
                return true;
            }
        }
        #endregion
    }

    /// <summary>
    /// 供料工位下一个步骤
    /// </summary>
    public enum GongLiaoStationRunStep
    {
        Step01_阻挡气缸伸出,
        Step02_皮带转动等待料盘到位,
        Step03_Z轴顶升_上下气缸_顶升,
        Step04_左右气缸_夹紧,
        Step05_前后气缸_夹紧,
        Step06_顶部视觉拍照_入料,
        Step07_等待取料完成,
        Step08_顶部视觉拍照_下料,
        Step09_前后_左右气缸_张开,
        Step10_Z轴顶升_上下气缸_下降,
        Step11_皮带转动等待下料完成,
        Setp12_过站_前后气缸张开,
        Setp13_过站_上下气缸_Z轴_阻挡下降,
        Setp14_过站_等待过站完成,
        Setp15_过站_上下气缸_Z轴上升,
        Setp16_过站_阻挡气缸伸出,
        Setp17_过站_前后气缸夹紧,
    }

    #region TODO 旧流程V2.0料盘同进同出  UPH2640 CT1.46
    //public class GongLiaoStation : StationBase<GongLiaoStationOption>, ISuportClearAlarm, IRefreshing
    //{
    //    #region  字段
    //    public volatile VisionStation Vision;
    //    public BuJinPiDai BuJinPiDaiStation;
    //    private readonly ICurrentRecipe _recipe;
    //    private const string Ds_顶升位 = "顶升位";
    //    private const string Ds_待机位 = "待机位";
    //    private const string Bj_待机位 = "待机位";
    //    private const string Bj_托盘位 = "托盘位";
    //    private bool feedTrayLoadError;
    //    private bool feedTrayUnloadError;
    //    private bool prouductNotRegisteredError;
    //    private bool prouductRegisteredClearError;
    //    private InspectorOption _inspectorOption;
    //    private bool _isInitialized;
    //    private ICalcTimeService _calcTimeService;
    //    private IOptionRepository _optionRepository;
    //    #endregion

    //    #region 事件
    //    public event EventHandler<string> StepsChanged;
    //    /// <summary>
    //    /// 人工确认
    //    /// </summary>
    //    public event Func<string, Task<bool>> ManualStepsConfirmChanged;
    //    #endregion

    //    #region 设备报警
    //    [Alarm(true, AlarmLevel.Error, "供料盘拍照报警-入料", Logical.Equal)]
    //    public bool FeedTrayLoadError
    //    {
    //        get => feedTrayLoadError;
    //        set => NotifyAlarm(ref feedTrayLoadError, value);
    //    }
    //    [Alarm(true, AlarmLevel.Error, "供料盘拍照报警-下料", Logical.Equal)]
    //    public bool FeedTrayUnloadError
    //    {
    //        get => feedTrayUnloadError;
    //        set => NotifyAlarm(ref feedTrayUnloadError, value);
    //    }
    //    [Alarm(true, AlarmLevel.Error, "供料盘未在籍", Logical.Equal)]
    //    public bool ProuductNotRegisteredError
    //    {
    //        get => prouductNotRegisteredError;
    //        set => NotifyAlarm(ref prouductNotRegisteredError, value);
    //    }
    //    [Alarm(true, AlarmLevel.Error, "供料盘在籍已清除,Tray盘未拿走", Logical.Equal)]
    //    public bool ProuductRegisteredClearError
    //    {
    //        get => prouductRegisteredClearError;
    //        set => NotifyAlarm(ref prouductRegisteredClearError, value);
    //    }
    //    #endregion

    //    #region 硬件设备
    //    public Electronic Electronic => Electronic.Default;
    //    [MotionPath(Bj_待机位)]
    //    [MotionPath(Bj_托盘位)]
    //    public DmcAxis BianJu_Axis { get; }
    //    [MotionPath(Ds_顶升位)]
    //    [MotionPath(Ds_待机位)]
    //    public DmcAxis DingSheng_Axis { get; }
    //    public BuJinPiDai PiDai { get; }
    //    [MotionPath(BuJinPiDai._运行高速)]
    //    [MotionPath(BuJinPiDai._运行低速)]
    //    public DmcAxis Axis_PiDai1 { get; }
    //    [MotionPath(BuJinPiDai._运行高速)]
    //    [MotionPath(BuJinPiDai._运行低速)]
    //    public DmcAxis Axis_PiDai2 { get; }
    //    public CylinderEx Cylinder_阻挡气缸1 => Electronic.Cylinder_入料到位阻挡气缸1;
    //    public CylinderEx Cylinder_阻挡气缸2 => Electronic.Cylinder_入料到位阻挡气缸2;
    //    public CylinderEx Cylinder_左右气缸1 => Electronic.Cylinder_入料左右定位气缸1;
    //    public CylinderEx Cylinder_左右气缸2 => Electronic.Cylinder_入料左右定位气缸2;
    //    public CylinderEx Cylinder_前后气缸1 => Electronic.Cylinder_入料前后定位水平气缸1;
    //    public CylinderEx Cylinder_前后气缸2 => Electronic.Cylinder_入料前后定位水平气缸2;
    //    public CylinderEx Cylinder_上下气缸1 => Electronic.Cylinder_入料前后定位上下气缸1;
    //    public CylinderEx Cylinder_上下气缸2 => Electronic.Cylinder_入料前后定位上下气缸2;
    //    #endregion

    //    #region 构造函数
    //    public GongLiaoStation()
    //    {
    //        Axis_PiDai1 = new(Electronic.Controller) { Id = nameof(Axis_PiDai1), NoId = 1, Name = "入料皮带步进1" };
    //        Axis_PiDai2 = new(Electronic.Controller) { Id = nameof(Axis_PiDai2), NoId = 2, Name = "入料皮带步进2" };
    //        PiDai = new(Axis_PiDai1, Axis_PiDai2) { Id = nameof(BuJinPiDai), Name = "入料皮带" };
    //        RetryCount = 0;
    //        this.InitalizeAlarms();
    //        LogGroup = LoggerCategory.GongLiao;
    //        BianJu_Axis = new(Electronic.Controller) { Id = nameof(BianJu_Axis), NoId = 0, Name = "入料变距伺服" };
    //        DingSheng_Axis = new(Electronic.Controller) { Id = "Gongliao" + nameof(DingSheng_Axis), NoId = 3, Name = "入料顶升步进", CanMove = IsFoolProofingJackingAxis };
    //        Cylinder_上下气缸1.CanMove = IsFoolProofingLoadTopBotCylinder;
    //        Cylinder_上下气缸2.CanMove = IsFoolProofingLoadTopBotCylinder;
    //        Add(BianJu_Axis, DingSheng_Axis);
    //        Add(PiDai.Axis_PiDai1, PiDai.Axis_PiDai2, PiDai);
    //        Add(Cylinder_阻挡气缸1);
    //        Add(Cylinder_阻挡气缸2);
    //        Add(Cylinder_左右气缸1);
    //        Add(Cylinder_左右气缸2);
    //        Add(Cylinder_前后气缸1);
    //        Add(Cylinder_前后气缸2);
    //        Add(Cylinder_上下气缸1);
    //        Add(Cylinder_上下气缸2);
    //    }
    //    #endregion

    //    #region 初始流程
    //    public bool IsInitialized => _isInitialized;
    //    /// <summary>
    //    /// 产品是否在籍
    //    /// </summary>
    //    public bool IsProuductRegistered => Status.HasFeedTray && Electronic.DI21_入料到位光电.Value;
    //    public async Task<bool> InitializeStation(CancellationToken cancellationToken)
    //    {
    //        try
    //        {
    //            OnStepChanged("初始化中");
    //            _isInitialized = false;
    //            LogInfo("初始化->[开始]");
    //            if (IsProuductRegistered == false)//产品不在籍情况
    //            {
    //                if (IsProductLine)//如果流水线有料
    //                {
    //                    if (Electronic.DI21_入料到位光电.Value == false && Electronic.DI39_入料到位减速光电.Value == false)//没有感应到产品
    //                    {
    //                        if (Cylinder_阻挡气缸1.IsBack())
    //                            await Cylinder_阻挡气缸1.PushOutAsync(token: cancellationToken);
    //                        if (Cylinder_阻挡气缸2.IsBack())
    //                            await Cylinder_阻挡气缸2.PushOutAsync(token: cancellationToken);
    //                    }
    //                    PiDai.AxisRun(cancellationToken, IsProductLine);
    //                    LogInfo($"初始化->[皮带上有料皮带转动]{IsProductLine}");
    //                    await Judger.Sure(() => Electronic.DI21_入料到位光电.Value, cancellationToken);
    //                    LogInfo($"初始化->[皮带上有料感应到位]{IsProductLine}");
    //                    await Task.Delay(motionTimeOption.ArriveSensorDelay, cancellationToken);
    //                    PiDai.AxisStop();
    //                    LogInfo($"初始化->[皮带上有料皮带停止]{IsProductLine}");
    //                }
    //                else
    //                {
    //                    if (Electronic.DI21_入料到位光电.Value == false && Electronic.DI39_入料到位减速光电.Value == false)//没有感应到产品
    //                    {
    //                        if (Cylinder_阻挡气缸1.IsBack())
    //                            await Cylinder_阻挡气缸1.PushOutAsync(token: cancellationToken);
    //                        if (Cylinder_阻挡气缸2.IsBack())
    //                            await Cylinder_阻挡气缸2.PushOutAsync(token: cancellationToken);
    //                    }
    //                    if (Cylinder_阻挡气缸1.IsPushOut() || Cylinder_阻挡气缸2.IsPushOut())
    //                    {
    //                        PiDai.AxisRun(cancellationToken, true);
    //                        LogInfo($"初始化->[皮带上阻挡气缸有伸出,空转5S]");
    //                        await Judger.Sure(() => Electronic.DI21_入料到位光电.Value, cancellationToken, timeout: 5_000);
    //                        await Task.Delay(motionTimeOption.ArriveSensorDelay, cancellationToken);
    //                        PiDai.AxisStop();
    //                        LogInfo($"初始化->[皮带上阻挡气缸有伸出,停止]");
    //                    }
    //                }
    //            }
    //            LogInfo($"初始化->[产品在籍{IsProuductRegistered}]");
    //            List<bool> init = new List<bool>();
    //            var boolA = await InitializeCylinder(cancellationToken);
    //            var boolB = await InitializeAxis(cancellationToken);
    //            if (IsProuductRegistered == false)//产品不在籍所以重新上一盘新的料
    //            {
    //                Step = GongLiaoStationRunStep.Step01_阻挡气缸伸出;
    //            }
    //            _isInitialized = boolA && boolB;
    //            if (_isInitialized)
    //            {
    //                OnStepChanged("初始化完成");
    //                LogInfo("初始化->[完成]");
    //            }
    //            else
    //            {
    //                OnStepChanged("初始化失败");
    //                LogInfo("初始化->[失败]");
    //            }
    //        }
    //        catch (Exception ex)
    //        {
    //            _isInitialized = false;
    //            OnStepChanged("初始化失败");
    //            LogError($"初始化->[异常]{ex.Message}", ex);
    //        }
    //        return _isInitialized;
    //    }
    //    /// <summary>
    //    /// 初始化气缸
    //    /// </summary>
    //    /// <param name="cancellationToken"></param>
    //    /// <returns></returns>
    //    async Task<bool> InitializeCylinder(CancellationToken cancellationToken)
    //    {
    //        var cylinders = Automatics.Where(x => x is CylinderEx).Cast<CylinderEx>().ToArray();
    //        bool bianJuInPosition = BianJu_Axis.CheckIsInPosition(Bj_托盘位);
    //        bool dingShenInPosition = DingSheng_Axis.CheckIsInPosition(Ds_顶升位);
    //        if (IsProuductRegistered && bianJuInPosition && dingShenInPosition)
    //        {
    //            var notCylinders = cylinders.Where(x => x.IsPushOut() == false);
    //            if (notCylinders.Any())
    //            {
    //                var notCylinderNames = notCylinders.Select(x => x.Name);
    //                LogError($"初始化->[{string.Join('|', notCylinderNames)}不在动点]");
    //                return false;
    //            }
    //            else
    //            {
    //                LogInfo($"初始化->[气缸已在动点]");
    //                return true;//产品是否在籍
    //            }
    //        }
    //        else
    //        {
    //            LogInfo($"初始化->[气缸原点开始]");
    //            await Task.WhenAll(Cylinder_前后气缸1.TakeBackAsync(token: cancellationToken), Cylinder_前后气缸2.TakeBackAsync(token: cancellationToken), Cylinder_左右气缸1.TakeBackAsync(token: cancellationToken), Cylinder_左右气缸1.TakeBackAsync(token: cancellationToken));
    //            if (!Cylinder_前后气缸1.IsBack() || !Cylinder_前后气缸2.IsBack())
    //            {
    //                LogError($"初始化->[前后气缸原点失败]");
    //                return false;
    //            }
    //            if (Electronic.DI21_入料到位光电.Value && Cylinder_阻挡气缸1.IsPushOut() && Cylinder_阻挡气缸2.IsPushOut())//表示流线到位这里有料,就不复位阻挡气缸了
    //            {
    //                await Task.WhenAll(Cylinder_上下气缸1.TakeBackAsync(token: cancellationToken), Cylinder_上下气缸2.TakeBackAsync(token: cancellationToken));
    //                if (!Cylinder_上下气缸1.IsBack() || !Cylinder_上下气缸2.IsBack())
    //                {
    //                    LogError($"初始化->[上下气缸原点失败]");
    //                    return false;
    //                }
    //            }
    //            else
    //            {
    //                var cylinders2 = cylinders.Where(x => x.IsPushOut());
    //                await Task.WhenAll(cylinders2.Select(x => x.TakeBackAsync(token: cancellationToken)));
    //                if (cylinders2.Any(cy => cy.IsBack() == false))
    //                {
    //                    LogError($"初始化->[气缸原点失败]");
    //                    return false;
    //                }
    //                LogInfo($"初始化->[气缸原点结束]");
    //            }
    //            return true;
    //        }
    //    }
    //    /// <summary>
    //    /// 初始化轴(产品在籍->不回原判断轴是不是在当前位|产品不在籍->回原所有轴，然后去各自待机位)
    //    /// </summary>
    //    /// <param name="cancellationToken"></param>
    //    /// <returns></returns>
    //    async Task<bool> InitializeAxis(CancellationToken cancellationToken)
    //    {
    //        var axies = Automatics.Where(x => x is DmcAxis).Cast<DmcAxis>().ToArray();
    //        LogInfo($"初始化->[轴重置开始]");
    //        axies.ForEach(x => x.Reset());
    //        axies.ForEach(x => x.ServoOn(true));
    //        LogInfo($"初始化->[轴重置结束]");
    //        bool bianJuInPosition = BianJu_Axis.CheckIsInPosition(Bj_托盘位);
    //        bool dingShenInPosition = DingSheng_Axis.CheckIsInPosition(Ds_顶升位);
    //        if (IsProuductRegistered)
    //        {
    //            if (!bianJuInPosition)
    //            {
    //                LogInfo($"初始化->[{BianJu_Axis.Name}[在籍]回原开始]");
    //                await BianJu_Axis.MoveOriginAsync(cancellationToken);
    //                LogInfo($"初始化->[{BianJu_Axis.Name}[在籍]回原结束]");
    //                //变距轴移动托盘位
    //                LogInfo($"初始化->[{BianJu_Axis.Name}移动->[在籍][Bj_托盘位]开始]");
    //                await BianJu_Axis.MoveToAsync(Bj_托盘位, cancellationToken);
    //                if (!BianJu_Axis.CheckIsInPosition(Bj_托盘位))
    //                {
    //                    LogError($"初始化->[{BianJu_Axis.Name}移动->[在籍][Bj_托盘位]失败]");
    //                    return false;
    //                }
    //                LogInfo($"初始化->[{BianJu_Axis.Name}移动->[在籍][Bj_托盘位]完成]");

    //            }
    //            if (!dingShenInPosition)
    //            {
    //                //LogError($"初始化->产品在籍,[{DingSheng_Axis.Name}未在->[Ds_顶升位]]");
    //                LogInfo($"初始化->[{DingSheng_Axis.Name}[在籍]回原开始]");
    //                await DingSheng_Axis.MoveOriginAsync(cancellationToken);
    //                LogInfo($"初始化->[{DingSheng_Axis.Name}[在籍]回原结束]");
    //                //顶升轴移动待机位
    //                LogInfo($"初始化->[{DingSheng_Axis.Name}移动->[在籍][Ds_待机位]开始]");
    //                await DingSheng_Axis.MoveToAsync(Ds_待机位, cancellationToken);
    //                if (!DingSheng_Axis.CheckIsInPosition(Ds_待机位))
    //                {
    //                    LogError($"初始化->[{DingSheng_Axis.Name}移动->[在籍][Ds_待机位]失败]");
    //                    return false;
    //                }
    //                LogInfo($"初始化->[{DingSheng_Axis.Name}移动->[在籍][Ds_待机位]完成]");

    //            }
    //            return true;
    //        }
    //        else
    //        {
    //            LogInfo($"初始化->[{DingSheng_Axis.Name}回原开始]");
    //            await DingSheng_Axis.MoveOriginAsync(cancellationToken);
    //            LogInfo($"初始化->[{DingSheng_Axis.Name}回原结束]");
    //            if (bianJuInPosition == false)//变距不在托盘位就回原
    //            {
    //                LogInfo($"初始化->[{BianJu_Axis.Name}回原开始]");
    //                await BianJu_Axis.MoveOriginAsync(cancellationToken);
    //                LogInfo($"初始化->[{BianJu_Axis.Name}回原结束]");
    //            }
    //            //变距轴移动托盘位
    //            LogInfo($"初始化->[{BianJu_Axis.Name}移动->[Bj_托盘位]开始]");
    //            await BianJu_Axis.MoveToAsync(Bj_托盘位, cancellationToken);
    //            if (!BianJu_Axis.CheckIsInPosition(Bj_托盘位))
    //            {
    //                LogError($"初始化->[{BianJu_Axis.Name}移动->[Bj_托盘位]失败]");
    //                return false;
    //            }
    //            LogInfo($"初始化->[{BianJu_Axis.Name}移动->[Bj_托盘位]完成]");
    //            //顶升轴移动待机位
    //            LogInfo($"初始化->[{DingSheng_Axis.Name}移动->[Ds_待机位]开始]");
    //            await DingSheng_Axis.MoveToAsync(Ds_待机位, cancellationToken);
    //            if (!DingSheng_Axis.CheckIsInPosition(Ds_待机位))
    //            {
    //                LogError($"初始化->[{DingSheng_Axis.Name}移动->[Ds_待机位]失败]");
    //                return false;
    //            }
    //            LogInfo($"初始化->[{DingSheng_Axis.Name}移动->[Ds_待机位]完成]");
    //        }
    //        PiDai.AxisStop();
    //        return true;
    //    }
    //    #endregion

    //    #region 运行流程
    //    public volatile GongLiaoRunningStatus Status;
    //    /// <summary>
    //    /// 运行步骤
    //    /// </summary>
    //    public GongLiaoStationRunStep Step
    //    {
    //        get => (GongLiaoStationRunStep)Status.Step;
    //        set
    //        {
    //            Status.Step = (int)value;
    //            Status.Save();
    //        }
    //    }
    //    /// <summary>
    //    /// 检查供料盘是否在籍
    //    /// </summary>
    //    /// <returns></returns>
    //    bool CheckProuductRegistered()
    //    {
    //        if (!Electronic.DI21_入料到位光电.Value)
    //        {
    //            if (prouductNotRegisteredError) return false;
    //            ProuductNotRegisteredError = true;
    //            LogError("供料盘未在籍");
    //            return false;
    //        }
    //        return true;
    //    }
    //    /// <summary>
    //    /// 检查供料盘是否被清理
    //    /// </summary>
    //    /// <returns></returns>
    //    bool CheckProuductRegisteredClear()
    //    {
    //        if (Electronic.DI21_入料到位光电.Value && (Cylinder_阻挡气缸1.IsBack() || Cylinder_阻挡气缸2.IsBack()))
    //        {
    //            if (prouductRegisteredClearError) return false;
    //            ProuductRegisteredClearError = true;
    //            return false;
    //        }
    //        return true;
    //    }
    //    /// <summary>
    //    /// 检查是否有产品进入流线当中
    //    /// </summary>
    //    bool IsProductLine => Electronic.DI19_入料防追尾光电.Value || Electronic.DI20_入料口光电.Value;
    //    GongLiaoMotionTimeOption motionTimeOption => _inspectorOption.GLMotionTimeOption;
    //    protected override async Task PreRunning()
    //    {
    //        try
    //        {
    //            var axisList = GetAxes();
    //            foreach (var axis in axisList)
    //            {
    //                await axis.InitializeAsync();
    //            }
    //            _optionRepository = Service.GetRequiredService<IOptionRepository>();
    //            _inspectorOption = _optionRepository.Get<InspectorOption>();
    //            Option = Recipe.Get<GongLiaoStationOption>(Id);
    //            Status = RunningStatus.GetRecord<GongLiaoRunningStatus>("GongLiao.json");
    //            if (_isInitialized == false)
    //            {
    //                throw new Exception("未初始化");
    //            }
    //            await RecoverPreRunning();
    //        }
    //        catch (Exception ex)
    //        {
    //            Root.Stop();
    //            LogError($"运行之前异常{ex.Message}", ex);
    //        }
    //    }
    //    protected override async Task RunningCoreAsync()
    //    {
    //        await Step01_阻挡气缸伸出();
    //        await Step02_皮带转动等待料盘到位();
    //        await Step03_Z轴顶升_上下气缸_顶升();
    //        await Step04_左右气缸_夹紧();
    //        await Step05_前后气缸_夹紧();
    //        await Step06_顶部视觉拍照_入料();
    //        await Step07_等待取料完成();
    //        await Step08_顶部视觉拍照_下料();
    //        await Step09_前后_左右气缸_张开();
    //        await Step10_Z轴顶升_上下气缸_下降();
    //        await Step11_皮带转动等待下料完成();
    //    }
    //    protected override void OnStopped()
    //    {
    //        var axies = GetAxes();
    //        axies.ForEach(x => x.PauseAsync());
    //        SaveRuningRecordState();
    //        PiDai.AxisStop();
    //    }
    //    async Task Step01_阻挡气缸伸出()
    //    {
    //        if (Step != GongLiaoStationRunStep.Step01_阻挡气缸伸出) return;
    //        OnStepChanged("Step01_阻挡气缸伸出");
    //        LogInfo("Step01_阻挡气缸伸出->[开始]");
    //        if (!CheckProuductRegisteredClear())
    //        {
    //            await Task.Delay(1000);
    //            return;
    //        }
    //        if (Cylinder_阻挡气缸1.IsBack() || Cylinder_阻挡气缸2.IsBack())
    //        {
    //            await Task.WhenAll(
    //            Cylinder_阻挡气缸1.PushOutAsync2(CancellationToken, motionTimeOption.CylinderPushOutTimeOut, motionTimeOption.CylinderPushOutDelay),
    //            Cylinder_阻挡气缸2.PushOutAsync2(CancellationToken, motionTimeOption.CylinderPushOutTimeOut, motionTimeOption.CylinderPushOutDelay));
    //        }
    //        LogInfo("Step01_阻挡气缸伸出->[结束]");
    //        Step = GongLiaoStationRunStep.Step02_皮带转动等待料盘到位;
    //    }
    //    async Task Step02_皮带转动等待料盘到位()
    //    {
    //        if (Step != GongLiaoStationRunStep.Step02_皮带转动等待料盘到位) return;
    //        OnStepChanged("Step02_皮带转动等待料盘到位");
    //        LogInfo("Step02_皮带转动等待料盘到位->[开始]");
    //        PiDai.AxisRun(CancellationToken, IsProductLine);
    //        if (DingSheng_Axis.CheckIsInPosition(Ds_待机位) == false)
    //        {
    //            throw new Exception("Step02_皮带转动等待料盘到位->[顶升Z轴未在待机位]");
    //        }
    //        await Judger.Sure(() => Electronic.DI21_入料到位光电.Value, CancellationToken);
    //        await Task.Delay(motionTimeOption.ArriveSensorDelay, CancellationToken);
    //        PiDai.AxisStop();
    //        LogInfo("Step02_皮带转动等待料盘到位->[结束]");
    //        Status.HasFeedTray = true;
    //        Step = GongLiaoStationRunStep.Step03_Z轴顶升_上下气缸_顶升;
    //    }
    //    async Task Step03_Z轴顶升_上下气缸_顶升()
    //    {
    //        if (Step != GongLiaoStationRunStep.Step03_Z轴顶升_上下气缸_顶升) return;
    //        OnStepChanged("Step03_Z轴顶升_上下气缸_顶升");
    //        LogInfo("Step03_Z轴顶升_上下气缸顶升->[开始]");
    //        if (!CheckProuductRegistered()) return;
    //        await Task.WhenAll(
    //            Cylinder_上下气缸1.PushOutAsync2(CancellationToken, motionTimeOption.CylinderPushOutTimeOut, motionTimeOption.CylinderPushOutDelay),
    //            Cylinder_上下气缸2.PushOutAsync2(CancellationToken, motionTimeOption.CylinderPushOutTimeOut, motionTimeOption.CylinderPushOutDelay),
    //            DingSheng_Axis.MoveToAsync(Ds_顶升位, CancellationToken));
    //        if (!CheckProuductRegistered()) return;
    //        if (!DingSheng_Axis.CheckIsInPosition(Ds_顶升位))
    //        {
    //            await Task.Delay(1000);
    //            throw new Exception("Step03_Z轴顶升_上下气缸顶升->[轴顶升位异常]");
    //        }
    //        if (!Cylinder_上下气缸1.IsPushOut() || !Cylinder_上下气缸2.IsPushOut())
    //        {
    //            await Task.Delay(1000);
    //            throw new Exception("Step03_Z轴顶升_上下气缸顶升->[气缸顶升异常]");
    //        }
    //        if (!CheckProuductRegistered()) return;
    //        LogInfo("Step03_Z轴顶升_上下气缸顶升->[结束]");
    //        Step = GongLiaoStationRunStep.Step04_左右气缸_夹紧;
    //    }
    //    async Task Step04_左右气缸_夹紧()
    //    {
    //        if (Step != GongLiaoStationRunStep.Step04_左右气缸_夹紧) return;
    //        OnStepChanged("Step04_左右气缸_夹紧");
    //        LogInfo("Step04_左右气缸_夹紧->[开始]");
    //        if (!CheckProuductRegistered()) return;
    //        await Task.WhenAll(
    //            Cylinder_左右气缸1.PushOutAsync2(CancellationToken, motionTimeOption.CylinderPushOutTimeOut, motionTimeOption.CylinderPushOutDelay),
    //            Cylinder_左右气缸2.PushOutAsync2(CancellationToken, motionTimeOption.CylinderPushOutTimeOut, motionTimeOption.CylinderPushOutDelay));
    //        if (!CheckProuductRegistered()) return;
    //        LogInfo("Step04_左右气缸_夹紧->[结束]");
    //        Step = GongLiaoStationRunStep.Step05_前后气缸_夹紧;
    //    }
    //    async Task Step05_前后气缸_夹紧()
    //    {
    //        if (Step != GongLiaoStationRunStep.Step05_前后气缸_夹紧) return;
    //        OnStepChanged("Step05_前后气缸_夹紧");
    //        LogInfo("Step05_前后气缸_夹紧->[开始]");
    //        if (!CheckProuductRegistered()) return;
    //        await Task.WhenAll(
    //            Cylinder_前后气缸1.PushOutAsync2(CancellationToken, motionTimeOption.CylinderPushOutTimeOut, motionTimeOption.CylinderPushOutDelay),
    //            Cylinder_前后气缸2.PushOutAsync2(CancellationToken, motionTimeOption.CylinderPushOutTimeOut, motionTimeOption.CylinderPushOutDelay));
    //        if (!CheckProuductRegistered()) return;
    //        LogInfo("Step05_前后气缸_夹紧->[结束]");
    //        _calcTimeService.CalcCT();//计算CT
    //        Step = GongLiaoStationRunStep.Step06_顶部视觉拍照_入料;
    //    }
    //    async Task Step06_顶部视觉拍照_入料()
    //    {
    //        if (Step != GongLiaoStationRunStep.Step06_顶部视觉拍照_入料) return;
    //        OnStepChanged("Step06_顶部视觉拍照_入料");
    //        LogInfo("Step06_顶部视觉拍照_入料->[开始]");
    //        if (!CheckProuductRegistered()) return;
    //        var result = await Vision.TopCapture();
    //        if (result.Quality != Quality.Ok)
    //        {
    //            LogError("Step06_顶部视觉拍照_入料->[拍照NG]");
    //            FeedTrayLoadError = true;
    //            await Task.Delay(1000);
    //            return;
    //        }
    //        if (!CheckProuductRegistered()) return;
    //        FeedTray.Instance.SetTrayItem(result.HasProducts);
    //        FeedTray.Instance.Save();
    //        SignalPanel.Signal.FeedTrayOffset = result.Offset;
    //        SignalPanel.Signal.CurrentFeedTray = result.Barcode;
    //        SignalPanel.Signal.FeedTrayReady = true;
    //        LogInfo("Step06_顶部视觉拍照_入料->[结束]");
    //        Step = GongLiaoStationRunStep.Step07_等待取料完成;
    //    }
    //    async Task Step07_等待取料完成()
    //    {
    //        if (Step != GongLiaoStationRunStep.Step07_等待取料完成) return;
    //        OnStepChanged("Step07_等待取料完成");
    //        LogInfo("Step07_等待取料完成->[开始]");
    //        if (!CheckProuductRegistered()) return;
    //        await Judger.Sure(() => SignalPanel.Signal.FeedTrayCompleted, CancellationToken);
    //        if (!CheckProuductRegistered()) return;
    //        SignalPanel.Signal.FeedTrayReady = false;
    //        SignalPanel.Signal.FeedTrayCompleted = false;
    //        LogInfo("Step07_等待取料完成->[结束]");
    //        Step = GongLiaoStationRunStep.Step08_顶部视觉拍照_下料;//Step = GongLiaoStationRunStep.Step09_前后_左右气缸_张开;
    //    }
    //    async Task Step08_顶部视觉拍照_下料()
    //    {
    //        if (Step != GongLiaoStationRunStep.Step08_顶部视觉拍照_下料) return;
    //        OnStepChanged("Step08_顶部视觉拍照_下料");
    //        LogInfo("Step08_顶部视觉拍照_下料->[开始]");
    //        if (!CheckProuductRegistered()) return;
    //        var result = await Vision.TopCapture(true);
    //        if (result.Quality != Quality.Ok)
    //        {
    //            LogError("Step08_顶部视觉拍照_下料->[视觉拍照失败]");
    //            FeedTrayUnloadError = true;
    //            await Task.Delay(1000);
    //            return;
    //        }
    //        var hasProducts = result.HasProducts;
    //        var feedTrayColunms = FeedTray.Instance.FeedTrayColunms;
    //        int i = 0;
    //        foreach (var colunm in feedTrayColunms)//用于判断那个是否料盘料是否有掉料防反
    //        {
    //            foreach (var item in colunm.Items)
    //            {
    //                if (item.Status != TrayPointStatus.Empty)
    //                {
    //                    var hasProduct = hasProducts[i];
    //                    if (!hasProduct)
    //                    {
    //                        FeedTrayUnloadError = true;
    //                        LogError("Step08_顶部视觉拍照_下料->[视觉拍照失败]");
    //                        await Task.Delay(1000);
    //                        return;
    //                    }
    //                }
    //                i++;
    //            }
    //        }
    //        LogInfo("Step08_顶部视觉拍照_下料->[结束]");
    //        Step = GongLiaoStationRunStep.Step09_前后_左右气缸_张开;
    //    }
    //    async Task Step09_前后_左右气缸_张开()
    //    {
    //        if (Step != GongLiaoStationRunStep.Step09_前后_左右气缸_张开) return;
    //        OnStepChanged("Step09_前后_左右气缸_张开");
    //        LogInfo("Step09_前后_左右气缸_张开->[开始]");
    //        if (!CheckProuductRegistered()) return;
    //        await Task.WhenAll(
    //            Cylinder_前后气缸1.TakeBackAsync2(CancellationToken, motionTimeOption.CylinderTakeBackTimeOut, motionTimeOption.CylinderTakeBackDelay),
    //            Cylinder_前后气缸2.TakeBackAsync2(CancellationToken, motionTimeOption.CylinderTakeBackTimeOut, motionTimeOption.CylinderTakeBackDelay),
    //            Cylinder_左右气缸1.TakeBackAsync2(CancellationToken, motionTimeOption.CylinderTakeBackTimeOut, motionTimeOption.CylinderTakeBackDelay),
    //            Cylinder_左右气缸2.TakeBackAsync2(CancellationToken, motionTimeOption.CylinderTakeBackTimeOut, motionTimeOption.CylinderTakeBackDelay));
    //        if (!CheckProuductRegistered()) return;
    //        LogInfo("Step09_前后_左右气缸_张开->[结束]");
    //        Step = GongLiaoStationRunStep.Step10_Z轴顶升_上下气缸_下降;
    //    }
    //    async Task Step10_Z轴顶升_上下气缸_下降()
    //    {
    //        if (Step != GongLiaoStationRunStep.Step10_Z轴顶升_上下气缸_下降) return;
    //        OnStepChanged("Step10_Z轴顶升_上下气缸_下降");
    //        LogInfo("Step10_Z轴顶升_上下气缸_下降->[开始]");
    //        if (!CheckProuductRegistered()) return;
    //        await Task.WhenAll(
    //            Cylinder_阻挡气缸1.TakeBackAsync2(CancellationToken, motionTimeOption.CylinderTakeBackTimeOut, motionTimeOption.CylinderTakeBackDelay),
    //            Cylinder_阻挡气缸2.TakeBackAsync2(CancellationToken, motionTimeOption.CylinderTakeBackTimeOut, motionTimeOption.CylinderTakeBackDelay),
    //            Cylinder_上下气缸1.TakeBackAsync2(CancellationToken, motionTimeOption.CylinderTakeBackTimeOut, motionTimeOption.CylinderTakeBackDelay),
    //            Cylinder_上下气缸2.TakeBackAsync2(CancellationToken, motionTimeOption.CylinderTakeBackTimeOut, motionTimeOption.CylinderTakeBackDelay),
    //            DingSheng_Axis.MoveToAsync(Ds_待机位, CancellationToken));
    //        if (!CheckProuductRegistered()) return;
    //        if (!DingSheng_Axis.CheckIsInPosition(Ds_待机位))
    //        {
    //            await Task.Delay(1000);
    //            throw new Exception("Step10_Z轴顶升_上下气缸_下降->[待机位异常]");
    //        }
    //        if (!Cylinder_上下气缸1.IsBack() || !Cylinder_上下气缸2.IsBack())
    //        {
    //            await Task.Delay(1000);
    //            throw new Exception("Step10_Z轴顶升_上下气缸_下降->[气缸下降异常]");
    //        }
    //        if (!CheckProuductRegistered()) return;
    //        LogInfo("Step10_Z轴顶升_上下气缸_下降->[结束]");
    //        Step = GongLiaoStationRunStep.Step11_皮带转动等待下料完成;
    //    }
    //    async Task Step11_皮带转动等待下料完成()
    //    {
    //        if (Step != GongLiaoStationRunStep.Step11_皮带转动等待下料完成) return;
    //        OnStepChanged("Step11_皮带转动等待下料完成");
    //        LogInfo("Step11_皮带转动等待下料完成->[开始]");
    //        PiDai.AxisRun(CancellationToken, IsProductLine);
    //        await Judger.Sure(() => Electronic.DI39_入料到位减速光电.Value, CancellationToken);
    //        LogInfo("Step11_皮带转动等待下料完成->[光电开]");
    //        await Judger.Sure(() => !Electronic.DI39_入料到位减速光电.Value, CancellationToken);
    //        LogInfo("Step11_皮带转动等待下料完成->[光电关]");
    //        await Judger.Sure(() => !Electronic.DI21_入料到位光电.Value && !Electronic.DI39_入料到位减速光电.Value, CancellationToken, 100);
    //        LogInfo("Step11_皮带转动等待下料完成->[结束]");
    //        Step = GongLiaoStationRunStep.Step01_阻挡气缸伸出;
    //        Status.HasFeedTray = false;
    //    }
    //    async Task Setp12_过站_前后气缸张开()
    //    {
    //        if (Step != GongLiaoStationRunStep.Setp12_过站_前后气缸张开) return;
    //        OnStepChanged("Setp12_过站_前后气缸张开");
    //        LogInfo("Setp12_过站_前后气缸张开->[开始]");
    //        await Task.WhenAll(
    //            Cylinder_前后气缸1.TakeBackAsync2(CancellationToken, motionTimeOption.CylinderTakeBackTimeOut, motionTimeOption.CylinderTakeBackDelay),
    //            Cylinder_前后气缸2.TakeBackAsync2(CancellationToken, motionTimeOption.CylinderTakeBackTimeOut, motionTimeOption.CylinderTakeBackDelay));
    //        LogInfo("Setp12_过站_前后气缸张开->[结束]");
    //        Step = GongLiaoStationRunStep.Setp13_过站_上下气缸_Z轴_阻挡下降;
    //    }
    //    async Task Setp13_过站_上下气缸_Z轴_阻挡下降()
    //    {
    //        if (Step != GongLiaoStationRunStep.Setp13_过站_上下气缸_Z轴_阻挡下降) return;
    //        OnStepChanged("Setp13_过站_上下气缸_Z轴_阻挡下降");
    //        LogInfo("Setp13_过站_上下气缸_Z轴_阻挡下降->[开始]");
    //        await Task.WhenAll(
    //          Cylinder_上下气缸1.TakeBackAsync2(CancellationToken, motionTimeOption.CylinderTakeBackTimeOut, motionTimeOption.CylinderTakeBackDelay),
    //          Cylinder_上下气缸2.TakeBackAsync2(CancellationToken, motionTimeOption.CylinderTakeBackTimeOut, motionTimeOption.CylinderTakeBackDelay),
    //          Cylinder_阻挡气缸1.TakeBackAsync2(CancellationToken, motionTimeOption.CylinderTakeBackTimeOut, motionTimeOption.CylinderTakeBackDelay),
    //          Cylinder_阻挡气缸2.TakeBackAsync2(CancellationToken, motionTimeOption.CylinderTakeBackTimeOut, motionTimeOption.CylinderTakeBackDelay),
    //         DingSheng_Axis.MoveToAsync(Ds_待机位));
    //        LogInfo("Setp13_过站_上下气缸_Z轴_阻挡下降->[结束]");
    //        Step = GongLiaoStationRunStep.Setp14_过站_等待过站完成;

    //    }
    //    async Task Setp14_过站_等待过站完成()
    //    {
    //        if (Step != GongLiaoStationRunStep.Setp14_过站_等待过站完成) return;
    //        OnStepChanged("Setp14_过站_等待过站完成");
    //        LogInfo("Setp14_过站_等待过站完成->[开始]");
    //        Electronic.Default.DI96_下料交互输入信号2模组继电器.Value = true;
    //        await Judger.Sure(() => Electronic.Default.DI96_下料交互输入信号2模组继电器.Value, CancellationToken);
    //        Electronic.Default.DI96_下料交互输入信号2模组继电器.Value = false;
    //        await Judger.Sure(() => Electronic.Default.DI96_下料交互输入信号2模组继电器.Value == false, CancellationToken);
    //        LogInfo("Setp14_过站_等待过站完成->[结束]");
    //        Step = GongLiaoStationRunStep.Setp15_过站_上下气缸_Z轴上升;

    //    }
    //    async Task Setp15_过站_上下气缸_Z轴上升()
    //    {
    //        if (Step != GongLiaoStationRunStep.Setp16_过站_阻挡气缸伸出) return;
    //        OnStepChanged("Setp15_过站_上下气缸_Z轴上升");
    //        LogInfo("Setp15_过站_上下气缸_Z轴上升->[开始]");
    //        await Task.WhenAll(
    //        Cylinder_上下气缸1.PushOutAsync2(CancellationToken, motionTimeOption.CylinderPushOutTimeOut, motionTimeOption.CylinderPushOutDelay),
    //        Cylinder_上下气缸2.PushOutAsync2(CancellationToken, motionTimeOption.CylinderPushOutTimeOut, motionTimeOption.CylinderPushOutDelay),
    //        DingSheng_Axis.MoveToAsync(Ds_顶升位));
    //        LogInfo("Setp15_过站_上下气缸_Z轴上升->[结束]");
    //        Step = GongLiaoStationRunStep.Setp16_过站_阻挡气缸伸出;

    //    }
    //    async Task Setp16_过站_阻挡气缸伸出()
    //    {
    //        if (Step != GongLiaoStationRunStep.Setp16_过站_阻挡气缸伸出) return;
    //        OnStepChanged("Setp16_过站_阻挡气缸伸出");
    //        LogInfo("Setp16_过站_阻挡气缸伸出->[开始]");
    //        await Task.WhenAll(
    //        Cylinder_阻挡气缸1.PushOutAsync2(CancellationToken, motionTimeOption.CylinderPushOutTimeOut, motionTimeOption.CylinderPushOutDelay),
    //        Cylinder_阻挡气缸1.PushOutAsync2(CancellationToken, motionTimeOption.CylinderPushOutTimeOut, motionTimeOption.CylinderPushOutDelay),
    //        DingSheng_Axis.MoveToAsync(Ds_顶升位));
    //        LogInfo("Setp16_过站_阻挡气缸伸出->[结束]");
    //        Step = GongLiaoStationRunStep.Setp17_过站_前后气缸夹紧;
    //    }
    //    async Task Setp17_过站_前后气缸夹紧()
    //    {
    //        if (Step != GongLiaoStationRunStep.Setp17_过站_前后气缸夹紧) return;
    //        OnStepChanged("Setp17_过站_前后气缸夹紧");
    //        LogInfo("Setp17_过站_前后气缸夹紧->[开始]");
    //        await Task.WhenAll(
    //        Cylinder_前后气缸1.PushOutAsync2(CancellationToken, motionTimeOption.CylinderPushOutTimeOut, motionTimeOption.CylinderPushOutDelay),
    //        Cylinder_前后气缸2.PushOutAsync2(CancellationToken, motionTimeOption.CylinderPushOutTimeOut, motionTimeOption.CylinderPushOutDelay),
    //        DingSheng_Axis.MoveToAsync(Ds_顶升位));
    //        LogInfo("Setp17_过站_前后气缸夹紧->[结束]");
    //        Step = GongLiaoStationRunStep.Step07_等待取料完成;
    //    }
    //    #endregion

    //    #region 状态恢复流程

    //    GongLiaoRunningStatus _preRunStatus;
    //    /// <summary>
    //    /// 状态恢复流程异常
    //    /// </summary>
    //    bool _isRecoverPreRunningError = false;
    //    /// <summary>
    //    /// 状态恢复标志位
    //    /// </summary>
    //    public bool RecoverPreStateFlag { get; private set; }
    //    /// <summary>
    //    /// 复到到停止运行之前检测状态
    //    /// </summary>
    //    /// <returns></returns>
    //    protected async Task RecoverPreRunning()
    //    {
    //        try
    //        {
    //            LogInfo($"运行状态恢复->[开始]");
    //            RecoverPreStateFlag = false;
    //            _isRecoverPreRunningError = false;
    //            _preRunStatus = Status.Clone();
    //            var isProuductRegistered = IsProuductRegistered;
    //            if (isProuductRegistered) //产品在籍恢复之前状态
    //            {
    //                if (Step == GongLiaoStationRunStep.Step02_皮带转动等待料盘到位)
    //                {
    //                    if (!DingSheng_Axis.CheckIsInPosition(Status.DSAxisPositions))//恢复顶升轴
    //                    {
    //                        await DingSheng_Axis.MoveToAsync(Status.DSAxisPositions);
    //                    }
    //                    //阻挡气缸恢复
    //                    await ManualCheckAsync(Cylinder_阻挡气缸1, Status.CylinderResist1);
    //                    await ManualCheckAsync(Cylinder_阻挡气缸2, Status.CylinderResist2);
    //                    //左右气缸恢复
    //                    if (!Cylinder_左右气缸1.VerifyState(Status.CylinderLeftRight1))
    //                        await Cylinder_左右气缸1.RecoveryState(Status.CylinderLeftRight1, CancellationToken);
    //                    if (!Cylinder_左右气缸2.VerifyState(Status.CylinderLeftRight2))
    //                        await Cylinder_左右气缸2.RecoveryState(Status.CylinderLeftRight2, CancellationToken);
    //                    //上下前后气缸恢复
    //                    await RecoverTopBotFrontBackCylinder(Cylinder_上下气缸1, Cylinder_前后气缸1, Status.CylinderTopBot1, Status.CylinderFront1);
    //                    await RecoverTopBotFrontBackCylinder(Cylinder_上下气缸2, Cylinder_前后气缸2, Status.CylinderTopBot2, Status.CylinderFront2);
    //                }
    //                else if (Step == GongLiaoStationRunStep.Step03_Z轴顶升_上下气缸_顶升)
    //                {
    //                    //阻挡气缸恢复
    //                    await ManualCheckAsync(Cylinder_阻挡气缸1, Status.CylinderResist1);
    //                    await ManualCheckAsync(Cylinder_阻挡气缸2, Status.CylinderResist2);
    //                    //左右气缸恢复
    //                    if (!Cylinder_左右气缸1.VerifyState(Status.CylinderLeftRight1))
    //                        await Cylinder_左右气缸1.RecoveryState(Status.CylinderLeftRight1, CancellationToken);
    //                    if (!Cylinder_左右气缸2.VerifyState(Status.CylinderLeftRight2))
    //                        await Cylinder_左右气缸2.RecoveryState(Status.CylinderLeftRight2, CancellationToken);
    //                    //上下前后气缸恢复
    //                    await RecoverTopBotFrontBackCylinder(Cylinder_上下气缸1, Cylinder_前后气缸1, Status.CylinderTopBot1, Status.CylinderFront1);
    //                    await RecoverTopBotFrontBackCylinder(Cylinder_上下气缸2, Cylinder_前后气缸2, Status.CylinderTopBot2, Status.CylinderFront2);
    //                }
    //                else if (Step == GongLiaoStationRunStep.Step04_左右气缸_夹紧 || Step == GongLiaoStationRunStep.Step05_前后气缸_夹紧)
    //                {
    //                    if (!DingSheng_Axis.CheckIsInPosition(Status.DSAxisPositions))//恢复顶升轴
    //                    {
    //                        await DingSheng_Axis.MoveToAsync(Status.DSAxisPositions);
    //                    }
    //                    //阻挡气缸恢复
    //                    await ManualCheckAsync(Cylinder_阻挡气缸1, Status.CylinderResist1);
    //                    await ManualCheckAsync(Cylinder_阻挡气缸2, Status.CylinderResist2);
    //                    if (Step == GongLiaoStationRunStep.Step05_前后气缸_夹紧)
    //                    {
    //                        //左右气缸恢复
    //                        if (!Cylinder_左右气缸1.VerifyState(Status.CylinderLeftRight1))
    //                            await Cylinder_左右气缸1.RecoveryState(Status.CylinderLeftRight1, CancellationToken);
    //                        if (!Cylinder_左右气缸2.VerifyState(Status.CylinderLeftRight2))
    //                            await Cylinder_左右气缸2.RecoveryState(Status.CylinderLeftRight2, CancellationToken);
    //                    }
    //                    //上下前后气缸恢复
    //                    await RecoverTopBotFrontBackCylinder(Cylinder_上下气缸1, Cylinder_前后气缸1, Status.CylinderTopBot1, Status.CylinderFront1);
    //                    await RecoverTopBotFrontBackCylinder(Cylinder_上下气缸2, Cylinder_前后气缸2, Status.CylinderTopBot2, Status.CylinderFront2);
    //                }
    //                if (Step == GongLiaoStationRunStep.Step06_顶部视觉拍照_入料 || Step == GongLiaoStationRunStep.Step07_等待取料完成 || Step == GongLiaoStationRunStep.Step08_顶部视觉拍照_下料)
    //                {
    //                    if (!DingSheng_Axis.CheckIsInPosition(Status.DSAxisPositions))//恢复顶升轴
    //                    {
    //                        await DingSheng_Axis.MoveToAsync(Status.DSAxisPositions);
    //                    }
    //                    if (!BianJu_Axis.CheckIsInPosition(Status.BJAxisPositions))//恢复变距轴
    //                    {
    //                        await BianJu_Axis.MoveToAsync(Status.BJAxisPositions);
    //                    }
    //                    //阻挡气缸恢复
    //                    await ManualCheckAsync(Cylinder_阻挡气缸1, Status.CylinderResist1);
    //                    await ManualCheckAsync(Cylinder_阻挡气缸2, Status.CylinderResist2);
    //                    //左右气缸恢复
    //                    if (!Cylinder_左右气缸1.VerifyState(Status.CylinderLeftRight1))
    //                        await Cylinder_左右气缸1.RecoveryState(Status.CylinderLeftRight1, CancellationToken);
    //                    if (!Cylinder_左右气缸2.VerifyState(Status.CylinderLeftRight2))
    //                        await Cylinder_左右气缸2.RecoveryState(Status.CylinderLeftRight2, CancellationToken);
    //                    //上下前后气缸恢复
    //                    await RecoverTopBotFrontBackCylinder(Cylinder_上下气缸1, Cylinder_前后气缸1, Status.CylinderTopBot1, Status.CylinderFront1);
    //                    await RecoverTopBotFrontBackCylinder(Cylinder_上下气缸2, Cylinder_前后气缸2, Status.CylinderTopBot2, Status.CylinderFront2);
    //                }
    //                else if (Step == GongLiaoStationRunStep.Step10_Z轴顶升_上下气缸_下降)
    //                {
    //                    //阻挡气缸恢复
    //                    await ManualCheckAsync(Cylinder_阻挡气缸1, Status.CylinderResist1);
    //                    await ManualCheckAsync(Cylinder_阻挡气缸2, Status.CylinderResist2);
    //                    //上下前后气缸恢复
    //                    await RecoverTopBotFrontBackCylinder(Cylinder_上下气缸1, Cylinder_前后气缸1, Status.CylinderTopBot1, Status.CylinderFront1);
    //                    await RecoverTopBotFrontBackCylinder(Cylinder_上下气缸2, Cylinder_前后气缸2, Status.CylinderTopBot2, Status.CylinderFront2);
    //                    //左右气缸恢复
    //                    if (!Cylinder_左右气缸1.VerifyState(Status.CylinderLeftRight1))
    //                        await Cylinder_左右气缸1.RecoveryState(Status.CylinderLeftRight1, CancellationToken);
    //                    if (!Cylinder_左右气缸2.VerifyState(Status.CylinderLeftRight2))
    //                        await Cylinder_左右气缸2.RecoveryState(Status.CylinderLeftRight2, CancellationToken);
    //                }
    //                else if (Step == GongLiaoStationRunStep.Step11_皮带转动等待下料完成)
    //                {
    //                    if (!DingSheng_Axis.CheckIsInPosition(Status.DSAxisPositions))//恢复顶升轴
    //                    {
    //                        await DingSheng_Axis.MoveToAsync(Status.DSAxisPositions);
    //                    }
    //                    //阻挡气缸恢复
    //                    await ManualCheckAsync(Cylinder_阻挡气缸1, Status.CylinderResist1);
    //                    await ManualCheckAsync(Cylinder_阻挡气缸2, Status.CylinderResist2);
    //                    //上下前后气缸恢复
    //                    await RecoverTopBotFrontBackCylinder(Cylinder_上下气缸1, Cylinder_前后气缸1, Status.CylinderTopBot1, Status.CylinderFront1);
    //                    await RecoverTopBotFrontBackCylinder(Cylinder_上下气缸2, Cylinder_前后气缸2, Status.CylinderTopBot2, Status.CylinderFront2);
    //                    //左右气缸恢复
    //                    if (!Cylinder_左右气缸1.VerifyState(Status.CylinderLeftRight1))
    //                        await Cylinder_左右气缸1.RecoveryState(Status.CylinderLeftRight1, CancellationToken);
    //                    if (!Cylinder_左右气缸2.VerifyState(Status.CylinderLeftRight2))
    //                        await Cylinder_左右气缸2.RecoveryState(Status.CylinderLeftRight2, CancellationToken);
    //                }
    //            }
    //            else //产品未在籍，初始化回到第一步
    //            {
    //                if (Step == GongLiaoStationRunStep.Step01_阻挡气缸伸出)
    //                {
    //                    if (!DingSheng_Axis.CheckIsInPosition(Ds_待机位))
    //                        await DingSheng_Axis.MoveToAsync(Ds_待机位);
    //                    if (Electronic.DI21_入料到位光电.Value == false && Electronic.DI39_入料到位减速光电.Value == false)//没有感应到产品
    //                    {

    //                    }
    //                    else
    //                    {
    //                        //阻挡气缸恢复
    //                        await ManualCheckAsync(Cylinder_阻挡气缸1, Status.CylinderResist1);
    //                        await ManualCheckAsync(Cylinder_阻挡气缸2, Status.CylinderResist2);
    //                    }
    //                    if (Cylinder_前后气缸1.IsPushOut() || Cylinder_前后气缸2.IsPushOut())
    //                    {
    //                        await Task.WhenAll(
    //                             Cylinder_前后气缸1.TakeBackAsync2(CancellationToken, motionTimeOption.CylinderTakeBackTimeOut, motionTimeOption.CylinderTakeBackDelay),
    //                             Cylinder_前后气缸2.TakeBackAsync2(CancellationToken, motionTimeOption.CylinderTakeBackTimeOut, motionTimeOption.CylinderTakeBackDelay));
    //                    }
    //                    if (Cylinder_上下气缸1.IsPushOut() || Cylinder_上下气缸2.IsPushOut())
    //                    {
    //                        await Task.WhenAll(
    //                             Cylinder_上下气缸1.TakeBackAsync2(CancellationToken, motionTimeOption.CylinderTakeBackTimeOut, motionTimeOption.CylinderTakeBackDelay),
    //                             Cylinder_上下气缸2.TakeBackAsync2(CancellationToken, motionTimeOption.CylinderTakeBackTimeOut, motionTimeOption.CylinderTakeBackDelay));
    //                    }
    //                }
    //                else if (Step == GongLiaoStationRunStep.Step02_皮带转动等待料盘到位)
    //                {
    //                    if (!BianJu_Axis.CheckIsInPosition(Bj_托盘位))
    //                        await BianJu_Axis.MoveToAsync(Bj_托盘位);
    //                    if (!DingSheng_Axis.CheckIsInPosition(Ds_待机位))
    //                        await DingSheng_Axis.MoveToAsync(Ds_待机位);
    //                    //阻挡气缸恢复
    //                    await ManualCheckAsync(Cylinder_阻挡气缸1, Status.CylinderResist1);
    //                    await ManualCheckAsync(Cylinder_阻挡气缸2, Status.CylinderResist2);
    //                    //左右气缸恢复
    //                    if (!Cylinder_左右气缸1.VerifyState(Status.CylinderLeftRight1))
    //                        await Cylinder_左右气缸1.RecoveryState(Status.CylinderLeftRight1, CancellationToken);
    //                    if (!Cylinder_左右气缸2.VerifyState(Status.CylinderLeftRight2))
    //                        await Cylinder_左右气缸2.RecoveryState(Status.CylinderLeftRight2, CancellationToken);
    //                    //上下前后气缸恢复
    //                    await RecoverTopBotFrontBackCylinder(Cylinder_上下气缸1, Cylinder_前后气缸1, Status.CylinderTopBot1, Status.CylinderFront1);
    //                    await RecoverTopBotFrontBackCylinder(Cylinder_上下气缸2, Cylinder_前后气缸2, Status.CylinderTopBot2, Status.CylinderFront2);
    //                }
    //                else
    //                {
    //                    if (Status.HasFeedTray)//产品有料在籍
    //                    {
    //                        if (!CheckProuductRegistered())
    //                        {
    //                            _isRecoverPreRunningError = false;
    //                            return;
    //                        }
    //                    }
    //                    var init = await InitializeStation(CancellationToken);
    //                    if (init)
    //                        Step = GongLiaoStationRunStep.Step01_阻挡气缸伸出;
    //                }
    //            }
    //            LogInfo($"运行状态恢复->[结束]");
    //            RecoverPreStateFlag = true;
    //        }
    //        catch (Exception ex)
    //        {
    //            _isRecoverPreRunningError = true;
    //            RecoverPreStateFlag = false;
    //            Root.Stop();
    //            LogError($"运行状态恢复->[异常]{ex.Message}", ex);
    //        }
    //    }
    //    async Task RecoverTopBotFrontBackCylinder(CylinderEx cylinderTopBot, CylinderEx cylinderFrontBack, CylinderStates cylinderTopBotState, CylinderStates cylinderFrontBackState)
    //    {
    //        if (cylinderTopBot.VerifyState(cylinderTopBotState) == false)
    //        {
    //            if (cylinderTopBotState == CylinderStates.PushOut)
    //            {
    //                if (cylinderFrontBack.IsPushOut())//判断前后气缸是否动位,先缩回原位
    //                    await cylinderFrontBack.TakeBackAsync();
    //                await cylinderTopBot.PushOutAsync();
    //                if (cylinderFrontBackState == CylinderStates.PushOut)
    //                {
    //                    await cylinderFrontBack.PushOutAsync();
    //                }
    //                else
    //                {
    //                    await cylinderFrontBack.TakeBackAsync();
    //                }
    //            }
    //            else
    //            {
    //                if (cylinderFrontBack.IsPushOut())
    //                    await cylinderFrontBack.TakeBackAsync();
    //                await cylinderTopBot.TakeBackAsync();
    //                if (cylinderFrontBackState == CylinderStates.PushOut)
    //                {
    //                    await cylinderFrontBack.PushOutAsync();
    //                }
    //                else
    //                {
    //                    await cylinderFrontBack.TakeBackAsync();
    //                }
    //            }
    //        }
    //        else
    //        {
    //            if (cylinderFrontBack.VerifyState(cylinderFrontBackState) == false)
    //            {
    //                if (cylinderFrontBackState == CylinderStates.PushOut)
    //                {
    //                    if (cylinderTopBot.IsPushOut())
    //                    {
    //                        await cylinderFrontBack.PushOutAsync();
    //                    }
    //                }
    //                else
    //                {
    //                    if (cylinderTopBot.IsPushOut())
    //                    {
    //                        await cylinderFrontBack.TakeBackAsync();
    //                    }
    //                }
    //            }
    //        }
    //    }
    //    async Task ManualCheckAsync(CylinderEx cylinder, CylinderStates cylinderState)
    //    {
    //        var msg = cylinderState == CylinderStates.PushOut ? "推出" : "收回";
    //        if (!cylinder.VerifyState(cylinderState))
    //        {
    //            var state = await OnManualStepsConfirm($"请确认[{cylinder.Name}]{msg}是否干涉?");
    //            if (state)
    //            {
    //                await cylinder.RecoveryState(cylinderState, CancellationToken);
    //            }
    //            else
    //            {
    //                throw new InvalidOperationException($"人工未确认[{cylinder.Name}]{msg}是否干涉!");
    //            }
    //        }
    //    }
    //    #endregion

    //    #region 公共方法
    //    public void OnStepChanged(string step)
    //    {
    //        StepsChanged?.SafeInvoke(this, step);
    //    }
    //    public async Task<bool> OnManualStepsConfirm(string message)
    //    {
    //        return await ManualStepsConfirmChanged.Invoke(message);
    //    }

    //    public override async Task InitializeAsync()
    //    {
    //        _calcTimeService = Service.GetRequiredService<ICalcTimeService>();
    //        _optionRepository = Service.GetRequiredService<IOptionRepository>();
    //        _inspectorOption = _optionRepository.Get<InspectorOption>();
    //        Status = RunningStatus.GetRecord<GongLiaoRunningStatus>("GongLiao.json");
    //        await base.InitializeAsync();
    //    }
    //    public void ClearAlarm()
    //    {
    //        FeedTrayLoadError = false;
    //        FeedTrayUnloadError = false;
    //        ProuductNotRegisteredError = false;
    //        ProuductRegisteredClearError = false;
    //    }
    //    public void Refreshing()
    //    {
    //        // 暂无实现
    //    }
    //    protected IEnumerable<DmcAxis> GetAxes()
    //    {
    //        return Automatics.Where(x => x is DmcAxis).Cast<DmcAxis>().ToArray();
    //    }
    //    #endregion

    //    #region 私有方法
    //    /// <summary>
    //    /// 检查令牌是否被取消
    //    /// </summary>
    //    /// <returns></returns>
    //    private async Task CheckCancellationToken()
    //    {
    //        await Task.Delay(1, CancellationToken);
    //    }
    //    void SaveRuningRecordState()
    //    {
    //        if (_isInitialized)
    //        {
    //            #region 阻挡气缸
    //            if (Cylinder_阻挡气缸1.IsPushOut())
    //                Status.CylinderResist1 = CylinderStates.PushOut;
    //            if (Cylinder_阻挡气缸1.IsBack())
    //                Status.CylinderResist1 = CylinderStates.TakeBack;

    //            if (Cylinder_阻挡气缸2.IsPushOut())
    //                Status.CylinderResist2 = CylinderStates.PushOut;
    //            if (Cylinder_阻挡气缸2.IsBack())
    //                Status.CylinderResist2 = CylinderStates.TakeBack;
    //            #endregion

    //            #region 前后气缸
    //            if (Cylinder_前后气缸1.IsPushOut())
    //                Status.CylinderFront1 = CylinderStates.PushOut;
    //            if (Cylinder_前后气缸1.IsBack())
    //                Status.CylinderFront1 = CylinderStates.TakeBack;

    //            if (Cylinder_前后气缸2.IsPushOut())
    //                Status.CylinderFront2 = CylinderStates.PushOut;
    //            if (Cylinder_前后气缸2.IsBack())
    //                Status.CylinderFront2 = CylinderStates.TakeBack;
    //            #endregion

    //            #region 上下气缸
    //            if (Cylinder_上下气缸1.IsPushOut())
    //                Status.CylinderTopBot1 = CylinderStates.PushOut;
    //            if (Cylinder_上下气缸1.IsBack())
    //                Status.CylinderTopBot1 = CylinderStates.TakeBack;

    //            if (Cylinder_上下气缸2.IsPushOut())
    //                Status.CylinderTopBot2 = CylinderStates.PushOut;
    //            if (Cylinder_上下气缸2.IsBack())
    //                Status.CylinderTopBot2 = CylinderStates.TakeBack;
    //            #endregion

    //            #region 左右气缸
    //            if (Cylinder_左右气缸1.IsPushOut())
    //                Status.CylinderLeftRight1 = CylinderStates.PushOut;
    //            if (Cylinder_左右气缸1.IsBack())
    //                Status.CylinderLeftRight1 = CylinderStates.TakeBack;

    //            if (Cylinder_左右气缸2.IsPushOut())
    //                Status.CylinderLeftRight2 = CylinderStates.PushOut;
    //            if (Cylinder_左右气缸2.IsBack())
    //                Status.CylinderLeftRight2 = CylinderStates.TakeBack;
    //            #endregion

    //            #region 轴点位
    //            Status.BJAxisPositions = BianJu_Axis.Current;
    //            Status.DSAxisPositions = DingSheng_Axis.Current;
    //            #endregion
    //        }
    //        #region 如果发生异常把之前状态赋值回去
    //        if (_isRecoverPreRunningError && _preRunStatus != null)
    //        {
    //            Status = _preRunStatus;
    //        }
    //        #endregion
    //        Status.Save();
    //    }
    //    //private void SaveRuningRecordStateInit()
    //    //{
    //    //    Status.BJAxisPositions = BianJu_Axis.Current;
    //    //    Status.DSAxisPositions = DingSheng_Axis.Current;
    //    //    Status.Save();
    //    //}
    //    #endregion

    //    #region 防呆方法
    //    /// <summary>
    //    /// 上下气缸防呆逻辑
    //    /// </summary>
    //    /// <returns></returns>
    //    bool IsFoolProofingLoadTopBotCylinder()
    //    {
    //        if (Electronic.DI19_入料防追尾光电.Value || Electronic.DI20_入料口光电.Value)
    //        {
    //            return false;
    //        }
    //        else if (Electronic.DI20_入料口光电.Value && Electronic.DI21_入料到位光电.Value)
    //        {
    //            return false;
    //        }
    //        else if (Electronic.DI32_入料前后拍板气缸1伸出磁开.Value || Electronic.DI34_入料前后拍板气缸2伸出磁开.Value)
    //        {
    //            return false;
    //        }
    //        else
    //        {
    //            return true;
    //        }
    //    }
    //    /// <summary>
    //    /// 顶升Z轴防呆逻辑
    //    /// </summary>
    //    /// <returns></returns>
    //    bool IsFoolProofingJackingAxis()
    //    {
    //        if (Electronic.DI19_入料防追尾光电.Value || Electronic.DI20_入料口光电.Value)
    //        {
    //            return false;
    //        }
    //        else if (Electronic.DI20_入料口光电.Value && Electronic.DI21_入料到位光电.Value)
    //        {
    //            return false;
    //        }
    //        else if (Electronic.DI39_入料到位减速光电.Value)
    //        {
    //            return false;
    //        }
    //        else
    //        {
    //            return true;
    //        }
    //    }
    //    #endregion
    //}
    #endregion

    #region TODO 旧流程
    //public class GongLiaoStation : StationBase<GongLiaoStationOption>, ISuportClearAlarm, IRefreshing
    //{
    //    #region Filed
    //    public volatile VisionStation Vision;
    //    public BuJinPiDai BuJinPiDaiStation;
    //    private readonly ICurrentRecipe _recipe;
    //    private const string Ds_顶升位 = "顶升位";
    //    private const string Ds_待机位 = "待机位";
    //    private const string Bj_待机位 = "待机位";
    //    private const string Bj_托盘位 = "托盘位";
    //    private bool _isCylinderError;
    //    private bool axisAlarm;
    //    private bool feedTrayError;
    //    private bool feedTrayUnloadError;
    //    private InspectorOption _inspectorOption;
    //    private static int _sensorDelay = 300;
    //    bool _isInitialized;
    //    private ICalcTimeService _calcTimeService;
    //    #endregion

    //    #region 硬件

    //    [MotionPath(Bj_待机位)]
    //    [MotionPath(Bj_托盘位)]
    //    public DmcAxis BianJu_Axis { get; }
    //    [MotionPath(Ds_顶升位)]
    //    [MotionPath(Ds_待机位)]
    //    public DmcAxis DingSheng_Axis { get; }
    //    public BuJinPiDai PiDai { get; }
    //    [MotionPath(BuJinPiDai._运行高速)]
    //    [MotionPath(BuJinPiDai._运行低速)]
    //    public DmcAxis Axis_PiDai1 { get; }
    //    [MotionPath(BuJinPiDai._运行高速)]
    //    [MotionPath(BuJinPiDai._运行低速)]
    //    public DmcAxis Axis_PiDai2 { get; }
    //    public CylinderEx Cylinder_阻挡气缸1 => Electronic.Cylinder_入料到位阻挡气缸1;
    //    public CylinderEx Cylinder_阻挡气缸2 => Electronic.Cylinder_入料到位阻挡气缸2;
    //    public CylinderEx Cylinder_左右气缸1 => Electronic.Cylinder_入料左右定位气缸1;
    //    public CylinderEx Cylinder_左右气缸2 => Electronic.Cylinder_入料左右定位气缸2;
    //    public CylinderEx Cylinder_前后气缸1 => Electronic.Cylinder_入料前后定位水平气缸1;
    //    public CylinderEx Cylinder_前后气缸2 => Electronic.Cylinder_入料前后定位水平气缸2;
    //    public CylinderEx Cylinder_上下气缸1 => Electronic.Cylinder_入料前后定位上下气缸1;
    //    public CylinderEx Cylinder_上下气缸2 => Electronic.Cylinder_入料前后定位上下气缸2;

    //    #endregion

    //    #region Property

    //    [Alarm(true, AlarmLevel.Error, "气缸不在正确位置", Logical.Equal)]
    //    public bool IsCylinderError { get => _isCylinderError; set => NotifyAlarm(ref _isCylinderError, value); }

    //    [Alarm(true, AlarmLevel.Error, "轴不在正确位置", Logical.Equal)]
    //    public bool AxisAlarm { get => axisAlarm; set => NotifyAlarm(ref axisAlarm, value); }

    //    [Alarm(true, AlarmLevel.Error, "供料盘拍照报警-入料", Logical.Equal)]
    //    public bool FeedTrayError { get => feedTrayError; set => NotifyAlarm(ref feedTrayError, value); }

    //    [Alarm(true, AlarmLevel.Error, "供料盘拍照报警-下料", Logical.Equal)]
    //    public bool FeedTrayUnloadError { get => feedTrayUnloadError; set => NotifyAlarm(ref feedTrayUnloadError, value); }
    //    public Electronic Electronic => Electronic.Default;


    //    public volatile GongLiaoRunningStatus Status;
    //    public event EventHandler<string> StepsChanged;
    //    /// <summary>
    //    /// 运行步骤
    //    /// </summary>
    //    public int Step
    //    {
    //        get => Status.Step; set
    //        {
    //            Status.Step = value;
    //            Status.Save();
    //        }
    //    }

    //    #endregion

    //    public GongLiaoStation()
    //    {
    //        Axis_PiDai1 = new(Electronic.Controller) { Id = nameof(Axis_PiDai1), NoId = 1, Name = "入料皮带步进1" };
    //        Axis_PiDai2 = new(Electronic.Controller) { Id = nameof(Axis_PiDai2), NoId = 2, Name = "入料皮带步进2" };
    //        PiDai = new(Axis_PiDai1, Axis_PiDai2) { Id = nameof(BuJinPiDai), Name = "入料皮带" };
    //        RetryCount = 0;
    //        this.InitalizeAlarms();
    //        LogGroup = LoggerCategory.GongLiao;
    //        BianJu_Axis = new(Electronic.Controller) { Id = nameof(BianJu_Axis), NoId = 0, Name = "入料变距伺服" };
    //        DingSheng_Axis = new(Electronic.Controller) { Id = "Gongliao" + nameof(DingSheng_Axis), NoId = 3, Name = "入料顶升步进", CanMove = IsZAxiesSave };
    //        Cylinder_上下气缸1.CanMove = LoadTopBotIsCylinderFaiDai;
    //        Cylinder_上下气缸2.CanMove = LoadTopBotIsCylinderFaiDai;
    //        Add(BianJu_Axis, DingSheng_Axis);
    //        Add(PiDai.Axis_PiDai1, PiDai.Axis_PiDai2, PiDai);
    //        this.Add(Cylinder_阻挡气缸1);
    //        this.Add(Cylinder_阻挡气缸2);
    //        this.Add(Cylinder_左右气缸1);
    //        this.Add(Cylinder_左右气缸2);
    //        this.Add(Cylinder_前后气缸1);
    //        this.Add(Cylinder_前后气缸2);
    //        this.Add(Cylinder_上下气缸1);
    //        this.Add(Cylinder_上下气缸2);
    //    }

    //    protected override async Task RunningCoreAsync()
    //    {
    //        try
    //        {
    //            await A_阻挡气缸伸出(0);
    //            await A_等待料盘(1);
    //            await A_料盘到位步进顶升(2);
    //            await A_定位料盘(3);
    //            #region TODO 没有加出料拍照流程
    //            await A_VisionCapture(4);
    //            await A_等待取料完成(5);
    //            await A_气缸缩回(6);
    //            await Action_顶升下降(7);
    //            await Action_等待料盘出料(8);
    //            #endregion
    //            #region TODO 有加出料拍照流程
    //            //await A_VisionCapture(4);
    //            //await A_等待取料完成(5);
    //            //await A_VisionCaptureUnolad(6);
    //            //await A_气缸缩回(7);
    //            //await Action_顶升下降(8);
    //            //await Action_等待料盘出料(9);
    //            #endregion
    //        }
    //        catch (Exception ex)
    //        {
    //            LogError(ex.Message);
    //        }
    //    }

    //    public async Task<bool> InitializeStation(CancellationToken cancellationToken)
    //    {
    //        try
    //        {
    //            LogInfo("开始初始化...");
    //            OnStepChanged("初始化中");
    //            var trayOnTop = Status.HasFeedTray && Cylinder_左右气缸1.IsPushOut() && Cylinder_左右气缸2.IsPushOut();
    //            var tasks = new List<Task<bool>>();
    //            if (!trayOnTop)
    //            {
    //                tasks.Add(Cylinder_前后气缸1.TakeBackAsync());
    //                tasks.Add(Cylinder_前后气缸2.TakeBackAsync());
    //                tasks.Add(Cylinder_阻挡气缸1.TakeBackAsync());
    //                tasks.Add(Cylinder_阻挡气缸2.TakeBackAsync());
    //                tasks.Add(Cylinder_左右气缸1.TakeBackAsync());
    //                tasks.Add(Cylinder_左右气缸2.TakeBackAsync());
    //                tasks.Add(Cylinder_上下气缸1.TakeBackAsync());
    //                tasks.Add(Cylinder_上下气缸2.TakeBackAsync());
    //            }
    //            var bools = await Task.WhenAll(tasks);
    //            if (bools.Any(x => !x))
    //            {
    //                LogError($"初始化失败,气缸缩回失败失败!");
    //                return false;
    //            }
    //            var axies = this.Automatics.Where(x => x is DmcAxis).Cast<DmcAxis>().ToArray();
    //            axies.ForEach(x => x.Reset());
    //            axies.ForEach(x => x.ServoOn(true));
    //            bool bianJuInPosition = BianJu_Axis.CheckIsInPosition(Bj_托盘位);
    //            bool dingShenInPosition = DingSheng_Axis.CheckIsInPosition(Ds_顶升位);
    //            LogInfo("等待顶升步进回原点,变距回原点");
    //            var tasks2 = new List<Task>();
    //            if (!dingShenInPosition || !trayOnTop)
    //            {
    //                tasks2.Add(DingSheng_Axis.MoveOriginAsync(cancellationToken));
    //            }
    //            if (!bianJuInPosition)
    //            {
    //                if (trayOnTop)
    //                {
    //                    LogError($"变距轴初始化失败,请先取走料盘，并清空料盘在籍!");
    //                    return false;
    //                }
    //                tasks2.Add(BianJu_Axis.MoveOriginAsync(cancellationToken));
    //            }
    //            await Task.WhenAll(tasks2);
    //            LogInfo("变距移动到托盘位");
    //            await BianJu_Axis.MoveToAsync(Bj_托盘位, cancellationToken);
    //            if (trayOnTop)
    //            {
    //                LogInfo("顶升步进回原点完成，移动到顶升位");
    //                await DingSheng_Axis.MoveToAsync(Ds_顶升位, cancellationToken);
    //                //Step = 3;
    //            }
    //            else
    //            {
    //                LogInfo("顶升步进回原点完成，移动到待机位");
    //                await DingSheng_Axis.MoveToAsync(Ds_待机位, cancellationToken);
    //                Step = 0;
    //            }
    //            PiDai.AxisStop();
    //            _isInitialized = true;
    //            LogInfo("初始化完成...");
    //            OnStepChanged("初始化完成");
    //            return true;
    //        }
    //        catch (Exception ex)
    //        {
    //            LogError($"{ex.Message}", ex);
    //            return false;
    //        }
    //    }

    //    private async Task A_阻挡气缸伸出(int step)
    //    {
    //        if (Step != step) return;
    //        LogInfo("开始伸出阻挡气缸");
    //        await Task.WhenAll(Cylinder_阻挡气缸1.PushOutAsync(), Cylinder_阻挡气缸2.PushOutAsync());
    //        LogInfo("阻挡气缸已伸出,开始皮带运行");
    //        var isYouliao = Electronic.DI19_入料防追尾光电.Value || Electronic.DI20_入料口光电.Value;
    //        PiDai.AxisRun(CancellationToken, isYouliao);
    //        Step = step + 1;
    //    }

    //    public async Task A_等待料盘(int step)
    //    {
    //        if (Step != step) return;
    //        OnStepChanged("等待料盘到位");
    //        LogInfo("皮带已开始运行,开始等待料盘到位");
    //        var isYouliao = Electronic.DI19_入料防追尾光电.Value || Electronic.DI20_入料口光电.Value;
    //        PiDai.AxisRun(CancellationToken, isYouliao);
    //        await Judger.Sure(() => Electronic.DI21_入料到位光电.Value, CancellationToken);
    //        await Task.Delay(500);
    //        LogInfo("料盘已到位，皮带停止运行");
    //        PiDai.AxisStop();
    //        Step = step + 1; ;
    //    }

    //    /// <summary>
    //    /// 等待入料到位后,顶升步进顶升
    //    /// </summary>
    //    /// <param name="step"></param>
    //    /// <returns></returns>
    //    public async Task A_料盘到位步进顶升(int step)
    //    {
    //        if (Step != step) return;
    //        OnStepChanged("顶升上升");
    //        LogInfo("皮带已停止运行,开始步进顶升");
    //        Task cylinder1 = Cylinder_上下气缸1.PushOutAsync();
    //        Task cylinder2 = Cylinder_上下气缸2.PushOutAsync();
    //        await DingSheng_Axis.MoveToAsync(Ds_顶升位, CancellationToken);
    //        await Task.WhenAll(cylinder1, cylinder2);
    //        var motionPath = DingSheng_Axis.FindMotionPath(Ds_顶升位);
    //        var min = motionPath.Target - 1;
    //        var max = motionPath.Target + 1;
    //        if (DingSheng_Axis.Current < min || DingSheng_Axis.Current > max)
    //        {
    //            await Task.Delay(1000);
    //            throw new Exception("料盘到位步进顶升异常");
    //        }

    //        //await Task.Delay(_inspectorOption.SensorOnDelay, CancellationToken);
    //        LogInfo("料盘到位步进顶升已完成,");
    //        Status.HasFeedTray = true;
    //        Step = step + 1; ;
    //    }

    //    /// <summary>
    //    /// 定位料盘，左右上下定位气缸伸出=》前后气缸伸出
    //    /// </summary>
    //    /// <param name="step"></param>
    //    /// <returns></returns>
    //    public async Task A_定位料盘(int step)
    //    {
    //        if (Step != step) return;
    //        OnStepChanged("定位料盘");
    //        LogInfo("开始伸出左右和上下气缸");
    //        //await Task.WhenAll(Cylinder_左右气缸1.PushOutAsync(token: CancellationToken), Cylinder_左右气缸2.PushOutAsync(token: CancellationToken), Cylinder_上下气缸1.PushOutAsync(token: CancellationToken), Cylinder_上下气缸2.PushOutAsync(token: CancellationToken));
    //        await Task.WhenAll(Cylinder_左右气缸1.PushOutAsync(token: CancellationToken), Cylinder_左右气缸2.PushOutAsync(token: CancellationToken));
    //        //await Task.Delay(_inspectorOption.SensorOnDelay);
    //        LogInfo("上下左右气缸已伸出,开始伸出前后气缸");
    //        await Task.WhenAll(Cylinder_前后气缸1.PushOutAsync(token: CancellationToken), Cylinder_前后气缸2.PushOutAsync(token: CancellationToken));
    //        Step = step + 1;
    //        _calcTimeService.CalcCT();//计算CT
    //    }

    //    /// <summary>
    //    /// 视觉拍照，成功后料盘到位
    //    /// </summary>
    //    /// <param name="step"></param>
    //    /// <returns></returns>
    //    public async Task A_VisionCapture(int step)
    //    {
    //        if (Step != step) return;
    //        OnStepChanged("顶部相机拍照-入料");
    //        LogInfo("料盘定位完成,开始拍照");
    //        var result = await Vision.TopCapture();
    //        if (result.Quality != Quality.Ok)
    //        {
    //            LogError("入料盘拍照失败!");
    //            FeedTrayError = true;
    //            await Task.Delay(1000);
    //            return;
    //        }
    //        FeedTray.Instance.SetTrayItem(result.HasProducts);
    //        SignalPanel.Signal.FeedTrayOffset = result.Offset;
    //        SignalPanel.Signal.CurrentFeedTray = result.Barcode;
    //        LogInfo("拍照完成，料盘到位就绪");
    //        SignalPanel.Signal.FeedTrayReady = true;
    //        Step = step + 1;
    //    }
    //    /// <summary>
    //    /// 撕膜完成，重新拍照确认料盘
    //    /// </summary>
    //    /// <param name="step"></param>
    //    /// <returns></returns>
    //    public async Task A_VisionCaptureUnolad(int step)
    //    {
    //        if (Step != step) return;
    //        OnStepChanged("顶部相机拍照-下料");
    //        LogInfo("下料盘定位完成,开始拍照");
    //        var result = await Vision.TopCapture(true);
    //        if (result.Quality != Quality.Ok)
    //        {
    //            LogError("下料盘拍照失败!");
    //            FeedTrayUnloadError = true;
    //            await Task.Delay(1000);
    //            return;
    //        }
    //        var hasProducts = result.HasProducts;
    //        var feedTrayColunms = FeedTray.Instance.FeedTrayColunms;
    //        int i = 0;
    //        foreach (var colunm in feedTrayColunms)//用于判断那个是否料盘防反
    //        {
    //            foreach (var item in colunm.Items)
    //            {
    //                if (item.Status != TrayPointStatus.Empty)
    //                {
    //                    var hasProduct = hasProducts[i];
    //                    if (!hasProduct)
    //                    {
    //                        FeedTrayUnloadError = true;
    //                        await Task.Delay(1000);
    //                        return;
    //                    }
    //                }
    //                i++;
    //            }
    //        }
    //        LogInfo("下料盘拍照结束");
    //        Step = step + 1;
    //    }

    //    /// <summary>
    //    /// 等待取料完成
    //    /// </summary>
    //    /// <param name="step"></param>
    //    /// <returns></returns>
    //    public async Task A_等待取料完成(int step)
    //    {
    //        if (Step != step) return;
    //        OnStepChanged("等待出料");
    //        LogInfo("开始等待取料完成");
    //        await Judger.Sure(() => SignalPanel.Signal.FeedTrayCompleted, CancellationToken);
    //        SignalPanel.Signal.FeedTrayReady = false;
    //        SignalPanel.Signal.FeedTrayCompleted = false;
    //        await Task.Delay(100);
    //        Step = step + 1; ;
    //    }

    //    /// <summary>
    //    /// 取料完成后，气缸缩回
    //    /// </summary>
    //    /// <param name="step"></param>
    //    /// <returns></returns>
    //    public async Task A_气缸缩回(int step)
    //    {
    //        if (Step != step) return;
    //        OnStepChanged("料盘解定位");
    //        LogInfo("取料已完成..开始缩回前后气缸和左右气缸...");
    //        await Task.WhenAll(Cylinder_前后气缸1.TakeBackAsync(token: CancellationToken), Cylinder_前后气缸2.TakeBackAsync(token: CancellationToken), Cylinder_左右气缸1.TakeBackAsync(token: CancellationToken), Cylinder_左右气缸2.TakeBackAsync());
    //        //LogInfo("前后左右气缸已缩回，开始缩回阻挡气缸和上下...");
    //        //await Task.WhenAll(Cylinder_阻挡气缸1.TakeBackAsync(token: CancellationToken), Cylinder_阻挡气缸2.TakeBackAsync(token: CancellationToken), Cylinder_上下气缸1.TakeBackAsync(token: CancellationToken), Cylinder_上下气缸2.TakeBackAsync(token: CancellationToken));
    //        Step = step + 1; ;
    //    }

    //    /// <summary>
    //    /// 顶升下降
    //    /// </summary>
    //    /// <param name="step"></param>
    //    /// <returns></returns>
    //    public async Task Action_顶升下降(int step)
    //    {
    //        if (Step != step) return;
    //        OnStepChanged("顶升下降");
    //        LogInfo("气缸已缩回,顶升移动到待机位...");
    //        Task cylinder1 = Cylinder_阻挡气缸1.TakeBackAsync(token: CancellationToken);
    //        Task cylinder2 = Cylinder_阻挡气缸2.TakeBackAsync(token: CancellationToken);
    //        Task cylinder3 = Cylinder_上下气缸1.TakeBackAsync(token: CancellationToken);
    //        Task cylinder4 = Cylinder_上下气缸2.TakeBackAsync(token: CancellationToken);
    //        await DingSheng_Axis.MoveToAsync(Ds_待机位, CancellationToken);
    //        var motionPath = DingSheng_Axis.FindMotionPath(Ds_待机位);
    //        var min = motionPath.Target - 1;
    //        var max = motionPath.Target + 1;
    //        if (DingSheng_Axis.Current < min || DingSheng_Axis.Current > max)
    //        {
    //            await Task.Delay(1000);
    //            throw new Exception("气缸已缩回,顶升移动到待机异常");
    //        }
    //        await Task.WhenAll(cylinder1, cylinder2, cylinder3, cylinder4);
    //        LogInfo("气缸已缩回,顶升移动到待机位完成");
    //        Step = step + 1; ;
    //    }

    //    /// <summary>
    //    /// 等待料盘出料
    //    /// </summary>
    //    /// <param name="step"></param>
    //    /// <returns></returns>
    //    public async Task Action_等待料盘出料(int step)
    //    {
    //        if (Step != step) return;
    //        OnStepChanged("料盘出料");
    //        LogInfo("顶升已移动到待机位，皮带运行，开始等待出料光电有信号");
    //        var isYouliao = Electronic.DI19_入料防追尾光电.Value || Electronic.DI20_入料口光电.Value;
    //        PiDai.AxisRun(CancellationToken);
    //        await Judger.Sure(() => Electronic.DI39_入料到位减速光电.Value, CancellationToken);
    //        LogInfo("出料光电有信号，开始等待出料口光电无信号");
    //        await Judger.Sure(() => !Electronic.DI39_入料到位减速光电.Value, CancellationToken);
    //        LogInfo("出料光电无信号，料盘出料完成");
    //        //await Task.Delay(2000);
    //        //await Judger.Sure(() => !Electronic.DI21_入料到位光电.Value && !Electronic.DI22_出料口光电.Value, CancellationToken, 100);
    //        await Judger.Sure(() => !Electronic.DI21_入料到位光电.Value && !Electronic.DI39_入料到位减速光电.Value, CancellationToken, 100);
    //        LogInfo("入到位光电无信号,出料光电无信号，料盘出料完成");
    //        Step = 0;
    //        Status.HasFeedTray = false;
    //    }

    //    public void ClearAlarm()
    //    {
    //        IsCylinderError = false;
    //        AxisAlarm = false;
    //        FeedTrayError = false;
    //    }

    //    public void OnStepChanged(string step)
    //    {
    //        StepsChanged?.SafeInvoke(this, step);
    //    }

    //    protected override void OnStopped()
    //    {
    //        var axies = this.Automatics.Where(x => x is DmcAxis).Cast<DmcAxis>().ToArray();
    //        axies.ForEach(x => x.PauseAsync());
    //        Status.Save();
    //        PiDai.AxisStop();
    //    }

    //    protected override async Task PreRunning()
    //    {
    //        try
    //        {
    //            foreach (var automatic in Automatics)
    //            {
    //                if (automatic is DmcAxis dmcAxis)
    //                {
    //                    await dmcAxis.InitializeAsync();
    //                }
    //            }
    //            Status = RunningStatus.GetRecord<GongLiaoRunningStatus>("GongLiao.json");
    //            var repository = Service.GetRequiredService<IOptionRepository>();
    //            Option = Recipe.Get<GongLiaoStationOption>(Id);
    //            _inspectorOption = repository.Get<InspectorOption>();
    //            if (!_isInitialized)
    //            {
    //                await InitializeStation(CancellationToken);
    //                _isInitialized = true;
    //            }
    //        }
    //        catch (Exception e)
    //        {
    //            LogError(e);
    //            Root.Stop();
    //        }
    //    }

    //    public override async Task InitializeAsync()
    //    {
    //        _calcTimeService = Service.GetRequiredService<ICalcTimeService>();
    //        var repository = Service.GetRequiredService<IOptionRepository>();
    //        _inspectorOption = repository.Get<InspectorOption>();
    //        Status = RunningStatus.GetRecord<GongLiaoRunningStatus>("GongLiao.json");
    //        await base.InitializeAsync();
    //    }

    //    public void Refreshing()
    //    {

    //    }

    //    public bool IsZAxiesSave()
    //    {
    //        if (Electronic.DI19_入料防追尾光电.Value || Electronic.DI20_入料口光电.Value)
    //        {
    //            return false;
    //        }
    //        else if (Electronic.DI20_入料口光电.Value && Electronic.DI21_入料到位光电.Value)
    //        {
    //            return false;
    //        }
    //        else
    //        {
    //            return true;
    //        }
    //    }
    //    /// <summary>
    //    /// 入料上下气缸
    //    /// </summary>
    //    /// <returns></returns>
    //    public bool LoadTopBotIsCylinderFaiDai()
    //    {
    //        if (Electronic.DI19_入料防追尾光电.Value || Electronic.DI20_入料口光电.Value)
    //        {
    //            return false;
    //        }
    //        else if (Electronic.DI20_入料口光电.Value && Electronic.DI21_入料到位光电.Value)
    //        {
    //            return false;
    //        }
    //        else if (Electronic.DI32_入料前后拍板气缸1伸出磁开.Value || Electronic.DI34_入料前后拍板气缸2伸出磁开.Value)
    //        {
    //            return false;
    //        }
    //        else
    //        {
    //            return true;
    //        }
    //    }
    //    public bool UnloadZuDangIsCylinderFaiDai()
    //    {
    //        if (Electronic.DI22_出料口光电.Value && Electronic.DI21_入料到位光电.Value)
    //        {
    //            return false;
    //        }
    //        else
    //        {
    //            return true;
    //        }
    //    }
    //}
    #endregion
}
